www.webdeveloper.com
Page 18 of 23 FirstFirst ... 81617181920 ... LastLast
Results 256 to 270 of 340

Thread: How to: Upload images using PHP

  1. #256
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    Hi guys hope somebody can help me out here, I have looked at the replies in this thread and tried a few different things, but I know virtually nothing about PHP or SQL so struggling to get it working as I would like.

    It uploads the image fine and also the new section I added that posts the vehicle details to SQL database works great, what I also need now though is for it to post file name in with the rest of the vehicle details.

    So I am able to add the image to the rest of the details when I make the request on the display page.

    Process code
    PHP Code:
    <?php 

    // filename: upload.processor.php 

    // first let's set some variables 

    // make a note of the current working directory, relative to root. 
    $directory_self str_replace(basename($_SERVER['PHP_SELF']), ''$_SERVER['PHP_SELF']); 

    // make a note of the directory that will recieve the uploaded file 
    $uploadsDirectory $_SERVER['DOCUMENT_ROOT'] . $directory_self 'uploaded_files/'

    // make a note of the location of the upload form in case we need it 
    $uploadForm 'http://' $_SERVER['HTTP_HOST'] . $directory_self 'upload.form.php'

    // make a note of the location of the success page 
    $uploadSuccess 'http://' $_SERVER['HTTP_HOST'] . $directory_self 'upload.success.php'

    // fieldname used within the file <input> of the HTML form 
    $fieldname 'file'

    // Now let's deal with the upload 

    // possible PHP upload errors 
    $errors = array(=> 'php.ini max file size exceeded'
                    
    => 'html form max file size exceeded'
                    
    => 'file upload was only partial'
                    
    => 'no file was attached'); 
                    
    // added section for form details
    $bike=$_POST['bike']; 
    $price=$_POST['price']; 
    $location=$_POST['location'];
    $details=$_POST['details']; 
    mysql_connect("localhost""username""Password") or die(mysql_error()); 
    mysql_select_db("test") or die(mysql_error()); 
    mysql_query("INSERT INTO `data` VALUES ('$bike', '$price', '$location', '$details')"); 


    // check the upload form was actually submitted else print the form 
    isset($_POST['submit']) 
        or 
    error('the upload form is neaded'$uploadForm); 

    // check for PHP's built-in uploading errors 
    ($_FILES[$fieldname]['error'] == 0
        or 
    error($errors[$_FILES[$fieldname]['error']], $uploadForm); 
         
    // check that the file we are working on really was the subject of an HTTP upload 
    @is_uploaded_file($_FILES[$fieldname]['tmp_name']) 
        or 
    error('not an HTTP upload'$uploadForm); 
         
    // validation... since this is an image upload script we should run a check   
    // to make sure the uploaded file is in fact an image. Here is a simple check: 
    // getimagesize() returns false if the file tested is not an image. 
    @getimagesize($_FILES[$fieldname]['tmp_name']) 
        or 
    error('only image uploads are allowed'$uploadForm); 
         
    // make a unique filename for the uploaded file and check it is not already 
    // taken... if it is already taken keep trying until we find a vacant one 
    // sample filename: 1140732936-filename.jpg 
    $now time(); 
    while(
    file_exists($uploadFilename $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])) 

        
    $now++; 


    // now let's move the file to its final location and allocate the new filename to it 
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename
        or 
    error('receiving directory insuffiecient permission'$uploadForm); 
        
        
         
    // If you got this far, everything has worked and the file has been successfully saved. 
    // We are now going to redirect the client to a success page. 
    header('Location: ' $uploadSuccess); 

    // The following function is an error handler which is used 
    // to output an HTML error page if the file upload fails 
    function error($error$location$seconds 5

        
    header("Refresh: $seconds; URL=\"$location\""); 
        echo 
    '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n"
        
    '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n"
        
    '<html lang="en">'."\n"
        
    '    <head>'."\n"
        
    '        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n"
        
    '        <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n"
        
    '    <title>Upload error</title>'."\n\n"
        
    '    </head>'."\n\n"
        
    '    <body>'."\n\n"
        
    '    <div id="Upload">'."\n\n"
        
    '        <h1>Upload failure</h1>'."\n\n"
        
    '        <p>An error has occured: '."\n\n"
        
    '        <span class="red">' $error '...</span>'."\n\n"
        
    '         The upload form is reloading</p>'."\n\n"
        
    '     </div>'."\n\n"
        
    '</html>'
        exit; 
    // end error handler 

    ?>

  2. #257
    Join Date
    Mar 2005
    Posts
    140
    Just put the section which posts the bike details AFTER the image filename is formed and just add a field for it in the sql table.

    Like, after this line:

    PHP Code:
    // now let's move the file to its final location and allocate the new filename to it
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
        or 
    error('receiving directory insuffiecient permission'$uploadForm); 

  3. #258
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    Hi cheers for your reply yeah I tried that nut it come back with to much info, example below of what $uploadFilename then sends to the database.

    /home/johnmev/public_html/uploaded_files/1242678878-ALFA.jpg

    Then when I try using this info to display the image it dont work, really need it with just the file name.

  4. #259
    Join Date
    Mar 2005
    Posts
    140
    Yeah, well you don't really need the whole URL - only the one after the "public_html", in your case: uploaded_files/1242678878-ALFA.jpg

    Because your "site" will not see anything beyond public_html folder.

  5. #260
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    How would I go about removing the rest from what I get off this script though?

    I take it I need to adjust the upload forms code of what data it collects?

    Cheers

  6. #261
    Join Date
    Mar 2005
    Posts
    140
    You can do this: save only the filename to database, and when retrieving it and displaying on the page just stick it the necessary path to the filename, taken from the database, like:

    PHP Code:
    $database_filename='1242678878-ALFA.jpg';
    $whole_path='uploaded_files/'.$database_filename

  7. #262
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    Erm surely that wont work though as I am making the variable database_filename = '1242678878-ALFA.jpg' wen I really need it to change each time a image is uploaded. So it saves the name of whatever image is uploaded to the database.

    I dont mind if its missing the file path altogether wen it saves the filename to the database, as I can add uploaded_files/ bit in HTML when I call for the file in the retrive script/file.

  8. #263
    Join Date
    Mar 2005
    Posts
    140
    This is what I meant. That was just an example - I just didn't want to write an example sql query and stuff. But anyway, you got the idea correctly =)

  9. #264
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    Sorry am stupid, erm still not sure wat it is I have got to do to the script I currently have, I dont know how to make it only write the filename to the database instead of its name and filepath.

  10. #265
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736

  11. #266
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    sorry to be a pain, that sorta works, only thing is its adding a dash infront of the file name and not the unquie number which was added to the photo upon upload.

  12. #267
    Join Date
    Jan 2006
    Location
    Stafford, England
    Posts
    565
    Quote Originally Posted by johneva View Post
    sorry to be a pain, that sorta works, only thing is its adding a dash infront of the file name and not the unquie number which was added to the photo upon upload.
    Infact scratch that it works great, I was just being dumb an putting it in the wrong place sorry.

    Another question for you though if somebody could help me out, how would I go about adding an id to each entery of the database, so each vehicle listed has a unquie ID number assigned to it on entery?
    Last edited by johneva; 05-19-2009 at 01:01 PM.

  13. #268
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by johneva View Post
    how would I go about adding an id to each entery of the database, so each vehicle listed has a unquie ID number assigned to it on entery?
    You should to start another thread in the database section to debate that. Most people would probably use AUTO_INCREMENT.

  14. #269
    Join Date
    May 2009
    Location
    England
    Posts
    5
    Hi all this is my first post.

    Came across your forum by chance, I am building a site for a friend and was searching for a script to enable him to upload images himself as he is not into dreamweaver etc and I don't want to be doing the uploading ever few days for him.
    That's the history now to my problem.
    Bokeh I have the script working like a dream but have tried a few mods without total success.

    What I have done is to put a select option drop down list on the upload.form page this is filled with Image1 Image2.................Image15 These will be the names I want to store the images as, the reason being since he will not have access to the images file over time this will be filled up with old images so if he only has let say 15 titles to pick from the images in the folder will always be current ones.
    The image name is being passed from the upload form into my database fine but so far I can't change the old file name with your extra numbers to be replaced with image1 or wahtever.
    I know that the reason for the extra number was to avoid duplicates but I would rather the old file was overwritten.

    Any help gratefully received as I am new to php and mysql having only worked on static sites so far.

    One other thing having worked my way through the 18 pages of this thread I see on page 17 a post about script has a major security vulnerability this is beyond me !
    I will have the script in a password protected folder have I any worries then?

    thanks
    Remus

  15. #270
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by Remus View Post
    I see on page 17 a post about script has a major security vulnerability
    That is a point of view of one person. Jpeg files contain comments and he is worried that someone might hide some php code in there and later the server may parse that jpeg file as php code. Pretty far fetched really and if the file extension is validated it is impossible.

    As for the file name just call it what you like. The method in this script was just an example of how to get a file name without collisions.
    Last edited by bokeh; 05-21-2009 at 02:22 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles