SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Image upload script - help

    Hi Guys,

    I found this old snippet of code that is used for uploading an image on my PC and need help 'deciphering' it.

    PHP Code:
    <?

        $root
    "";
        
    $webroot "";
        
    $path "";
        

        if (!isset(
    $HTTP_POST_FILES['userfile'])) exit;

        if (
    is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) 
        {
            if (
    file_exists($path $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }
        
            
    $res copy($HTTP_POST_FILES['userfile']['tmp_name'], $root.$path.$HTTP_POST_FILES['userfile']['name']);

            if (!
    $res) { echo "upload failed!<br>\n"; exit; } else { echo "upload sucessful<br>\n"; }

            echo 
    "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
            echo 
    "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
            echo 
    "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
            echo 
    "<a href=\"$webroot$path".$HTTP_POST_FILES['userfile']['name']."\">View File</a><br>";
            echo 
    "<a href=\"JavaScript:window.close()\">Close Window</a>";

        } 
        else 
        { 
            echo 
    "Wrong file type<br>\n"; exit; 
        }
        
        
    ?>
    Can anyone possibly give me examples of what I should set the following as:

    Upload images to: mydomain.com/uploaded-images/
    Script is located at: mydomain.com/admin/


    PHP Code:
    $root"";
    $webroot "";
    $path ""

  2. #2
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I decided to use this instead, seems to do the job

    PHP Code:
    // Check for an uploaded file.
        
    if (isset($_FILES['picture'])) {
        
            
    // Validate the type. Should be jpeg, jpg, or gif. 
            
    $allowed = array ('image/gif''image/jpeg''image/jpg''image/pjpeg'); 
            if (
    in_array($_FILES['picture']['type'], $allowed)) 
            { 


    //  If all is ok begin picture uploading 
        
    $picDIR "$UploadDirectory";    // set upload directory for large image (set in config file)
        
    $picture_name time() . $_FILES['picture']['name'];  // rename the picture
        
    move_uploaded_file($_FILES['picture']['tmp_name'], $picDIR $picture_name)      //  upload picture 
            
    or die ("The image upload failed");
        
        
     
    // If all went ok, print success message   
        
    echo "The image was successfuly uploaded.";  



    // Invalid file upload type.
        
    } else { 
        echo 
    '<p><font color="red">Please ensure the pictures are JPEG or GIF images.</font></p>';
        
    unlink ($_FILES['picture']['tmp_name']); // Delete the file.
        
    exit;
            }
                
    // No file uploaded.        
    } else { 
        echo 
    'There has been an error with the file upload process!';
        exit;
        } 

  3. #3
    SitePoint Guru adammc's Avatar
    Join Date
    Aug 2004
    Location
    Cairns, Australia
    Posts
    762
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One question though.
    If I was to use this on a form to update / edit a database entry.... This code wont let me leave the picture the same (to not upload a picture again)

    It spits out errors if I select not to change it.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adammc View Post
    One question though.
    If I was to use this on a form to update / edit a database entry.... This code wont let me leave the picture the same (to not upload a picture again)

    It spits out errors if I select not to change it.
    If you want to edit the database and not upload an image endless there is an image posted in the iamge upload field... I would do this.

    Code PHP:
    if(isset($_POST['update'])){
       if(!empty($_FILES['yourfile']){
        // all your file upload code here
       }
       // other sql statements here that run regardless if the file field was empty
    }


Bookmarks

Posting Permissions

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