Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Php Uploading Images

  • 30-03-2011 12:16PM
    #1
    Registered Users, Registered Users 2 Posts: 378 ✭✭


    Hi guys

    As part of my website, you are able to upload a profile picture

    I have this working locally using WAMP and its working fine

    The upload directory is set to "uploaded_files"

    My ISP has assured me that everyone has full read/write access to this directory, however when I upload the file, I get the error "receiving directory insuffiecient permission", this message is controled in my php code

    The error occurs when trying to move the file to the location using move_uploaded_file

    Here is my code, (any help or guidance is greatly appreciated)

    <?php

    //require_once "header.php";

    $username = $_SESSION;

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

    // make a note of the directory that will recieve the uploaded files
    $uploadsDirectory = $_SERVER . $directory_self . 'uploaded_files/';

    // make a note of the location of the upload form in case we need it
    $uploadForm = 'http://' . $_SERVER . $directory_self . 'edit-profile.php';

    // make a note of the location of the success page
    $uploadSuccess = 'http://' . $_SERVER . $directory_self . 'upload.success.php';

    // name of the fieldname used for the file in the HTML form
    $fieldname = 'file';



    // Now let's deal with the upload

    // possible PHP upload errors
    $errors = array(1 => 'php.ini max file size exceeded',
    2 => 'HTML form max file size exceeded',
    3 => 'File upload was only partial',
    4 => 'No file was attached');

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

    // check for PHP's built-in uploading errors
    ($_FILES[$fieldname] == 0)
    or error($errors[$_FILES[$fieldname]], $uploadForm);


    // check that the file we are working on really was an HTTP upload
    @is_uploaded_file($_FILES[$fieldname])
    or error('Not an HTTP upload', $uploadForm);

    // validation... since this is an image upload script we
    // should run a check to make sure the upload is an image
    @getimagesize($_FILES[$fieldname])
    or error('Only image uploads are allowed', $uploadForm);

    //assign the filname with username.fileextension

    $uploadFilename = $uploadsDirectory.$username.'.'.pathinfo($_FILES[$fieldname],PATHINFO_EXTENSION);

    // setting the variable to be the name of the file rather than the full path
    $profilepic = basename($uploadFilename);

    // now let's move the file to its final and allocate it with the new filename
    @move_uploaded_file($_FILES[$fieldname], $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 the success page.


    $sql = ("UPDATE `profile` SET `image` = '$profilepic' WHERE `profile`.`username` = '$username' LIMIT 1");

    mysql_query($sql) or die ("Error: ".mysql_error());

    header('Location: ' . $uploadSuccess);

    // make an error handler which will be used if the upload fails
    function error($error, $location, $seconds = 3)
    {
    //header("Refresh: $seconds; URL=\"$location\"");
    echo '<center><table id= "DontWantToHide" class="upload-error">
    <tr><td> </tr></td>
    <tr><td class="upload-error">Upload Error</td></tr>
    <tr><td class="upload-error">'.$error.'</td></tr>
    <tr><td class="upload-error">Profile Page is Reloading</tr></td>
    </table></center>

    <meta http-equiv="refresh" content="3;url=profile.php" />';

    exit;
    } // end error handler






    ?>


Comments

  • Registered Users, Registered Users 2 Posts: 379 ✭✭TheWaterboy


    Maybe try putting this after the line you define $uploadsDirectory:

    chmod($uploadsDirectory,0777);

    Then at the end of your script put:

    chmod($uploadsDirectory,0755);

    Also check that the path you are using for the upload directory is correct.

    Maybe remove @ from @move_uploaded_file and check your logs to get the PHP error been logged.


  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    ***Post Removed***

    Issue resolved, thank you for all your help


Advertisement