SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image Upload Script Showing Erro

    I'm using the follwing script to allow someone to upload an image to my server.

    All I want to do is have the filename changed to the "HHHHMMYY-HHMMSS-userID" and uploaded into "/images/userpics/" I have 777'd both of those folders but still it's coming up with "Failed to upload file!"

    Here's the script...
    PHP Code:
    $datenow gmdate('Ymd');
    $timenow gmdate('His');

    $ext substr($userfile_namestrrpos($userfile_name'.'));
    $userfile_name $datenow '-' $timenow '-' $myuID.$ext;
    $add $_SERVER['DOCUMENT_ROOT'].'/images/userpics/'.$userfile_name;

    if(!
    move_uploaded_file ($userfile$add)){
        echo 
    "Failed to upload file!";
    } else {
        
    $query "
            INSERT INTO photos
            VALUES ('',
                '
    $myuID',
                '',
                '
    $userfile_name',
                '
    $caption',
                '
    $datetimenow')";
        
    mysql_query($query) or die(mysql_error());

        
    header("Location: /account.php");

    Thanks
    Ian

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show us here the HTML form as well as your whole PHP code where the variables $userfile_name and $userfile are filled?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the whole of the PHP script:
    PHP Code:
    if ($_POST['action'] == 'uploadphoto') {
        
    $datenow gmdate('Ymd');
        
    $timenow gmdate('His');

        
    $ext substr($userfile_namestrrpos($userfile_name'.'));
        
    $userfile_name $datenow '-' $timenow '-' $myuID.$ext;
        
    $add $_SERVER['DOCUMENT_ROOT'].'/images/userpics/'.$userfile_name;

        if(!
    move_uploaded_file ($userfile$add)){
            echo 
    "Failed to upload file. Contact Site admin to fix the problem";
        } else {
            
    $query "
                INSERT INTO photos
                VALUES ('',
                    '
    $myuID',
                    '',
                    '
    $userfile_name',
                    '
    $caption',
                    '
    $datetimenow')";
            
    mysql_query($query) or die(mysql_error());

            
    header("Location: /account.php");
        }

    and here is the upload form:
    HTML Code:
    <table width="100%" cellspacing="0" cellpadding="3">
    <form name="AddPhoto" method="post" action="/account.php" enctype="multipart/form-data">
    <input type="hidden" name="action" value="uploadphoto">
    <tr>
        <td colspan="3"><h1>Upload Profile Photo</h1></td>
    </tr><tr>
        <td width="150"><p>Locate Photo</p></td>
        <td width="100" align="right"><p>*</p></td>
        <td><p><input type="file" style="width:180px;" name="userfile"></p></td>
    </tr><tr>
        <td><p></p></td>
        <td align="right"><p></p></td>
        <td><p><input type="submit" value="Upload Photo"></p></td>
    </tr>
    </form>
    </table>

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK then try out this PHP code for uploading which must work for you. I have just made some changes on your code but not tested.
    PHP Code:
    if ($_POST['action'] == 'uploadphoto') {
        
    $datenow gmdate('Ymd');
        
    $timenow gmdate('His');
        
        
    $filename basename($_FILES['userfile']['name']);
        if(
    $_FILES['userfile']['type'] == "image/jpeg" || $_FILES['userfile']['type'] == "pjpeg")
            
    $ext ".jpg";
        else if(
    $_FILES['userfile']['type'] == "image/gif")
            
    $ext ".gif";
        else if(
    $_FILES['userfile']['type'] == "image/png")
            
    $ext ".png";
        
        
    //$ext = substr($filename, strrpos($filename, '.'));
        
        
    $userfile_name $datenow '-' $timenow '-' $myuID $ext;
        
        
    $add $_SERVER['DOCUMENT_ROOT'].'/images/userpics/' $userfile_name;

        if(!
    move_uploaded_file($_FILES['userfile']['tmp_name'], $add)){
            echo 
    "Failed to upload file. Contact Site admin to fix the problem";
        } else {
            
    $query "
                INSERT INTO photos
                VALUES ('',
                    '
    $myuID',
                    '',
                    '
    $userfile_name',
                    '
    $caption',
                    '
    $datetimenow')";
            
    mysql_query($query) or die(mysql_error());

            
    header("Location: ./account.php");
            exit();
        }

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rajug - You're the man!!! Thanks so much. I can't think why it doesn't work on this site as it worked fine on an old site of mine, unless the server has changed at all.

    Anyways, all working correctly now

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh then if this code was working in earlier site means there was the PHP version less than 4. Because we can use the HTML form elements in earlier version just adding a $ dollar symbol at the beginning of name. Like if you have text field in the form named "txtValue" then you could use $txtValue in the PHP posted script. Likewise your code was correct in the case of PHP older version.

    Ok now study about such changes and updates on modern PHP versions.

    Good luck man.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've noticed that in a few of the older sites I made, they all started returning errors. I never really thought about it much.

    Ta
    Ian


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
  •