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 File Upload script not working

  • 07-12-2011 06: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,449 ✭✭✭✭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