Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP File Upload script not working

  • 07-12-2011 6:19pm
    #1
    Registered Users, Registered Users 2 Posts: 806 ✭✭✭


    Hi,

    I'm having some trouble uploading and renaming an image file that I upload through a form on my website.

    When I submit the form, it is passed to a function which then passes it to a method in the Article class (it is a news image).

    So when the article is submitted, it is written into the database and a method to upload files is called.

    [PHP]$article->fileUpload();[/PHP]

    [PHP]
    public static function fileUpload() {
    $filename = $_FILES["file"]["name"];
    $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
    $file_ext = substr($filename, strripos($filename, '.')); // get file name
    $filesize = $_FILES["file"]["size"];
    $allowed_file_types = array('.jpg','.jpeg','.gif','.png');

    if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000)) {

    // rename file
    $newfilename = $this->id . $file_ext;

    if (file_exists("uploads/news_images/" . $newfilename)) {
    // file already exists error
    echo "You have already uploaded this file.";
    } else {
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/news_images/" . $newfilename);
    echo "File uploaded successfully.";
    }

    } elseif (empty($file_basename)) {
    // file selection error
    echo "Please select a file to upload.";
    } elseif ($filesize > 200000) {
    // file size error
    echo "The file you are trying to upload is too large.";
    } else {
    // file type error
    echo "Only these file types are allowed for upload: " . implode(', ',$allowed_file_types);
    unlink($_FILES["file"]["tmp_name"]);
    }

    }[/PHP]

    The script is pretty much the same as this: http://zacvineyard.com/blog/2010/03/15/a-better-php-upload-and-rename-script/


Comments

  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    check your error.log, 90% chance that it tells you the problem.


  • Registered Users, Registered Users 2 Posts: 806 ✭✭✭Niall09


    Forgot to close a quote in the form action, got it sorted now, thanks! :o


Advertisement