1

I am setting up a file upload service on my website. Here is what I got so far,

upload.php

<form action="uploader.php" method="post" enctype="multipart/form-data"> 
 <input type="file" name="myFile">
 <br>
 <input type="submit" value="Upload">
</form>

uploader.php

<?php
define("UPLOAD_DIR", "/uploads");

if (!empty($_FILES["myFile"])) {
    $myFile = $_FILES["myFile"];

    if ($myFile["error"] !== UPLOAD_ERR_OK) {
        echo "<p>An error occurred.</p>";
        exit;
    }


    // ensure a safe filename
    $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

    // verify the file is a GIF, JPEG, or PNG
    $fileType = exif_imagetype($_FILES["myFile"]["tmp_name"]);
    $allowed = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
    if (!in_array($fileType, $allowed)) {
        // file type is not permitted
        echo "<p>Unable to save file.</p>";
        exit;
    }



    // don't overwrite an existing file
    $i = 0;
    $parts = pathinfo($name);
    while (file_exists(UPLOAD_DIR . $name)) {
        $i++;
        $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
    }

    // preserve file from temporary directory
    $success = move_uploaded_file($myFile["tmp_name"],
        UPLOAD_DIR . $name);
    if (!$success) { 
        echo "<p>Unable to save file.</p>";
        exit;
    }

    // set proper permissions on the new file
    chmod(UPLOAD_DIR . $name, 0644);
}

There is a folder in my directory called uploads, I want my files to upload to there.

However when running it using XAMMP, I try and upload a image called example.png and it comes up with the following errors.

Warning: move_uploaded_file(/uploadsexample.png): failed to open stream: Permission denied in C:\xampp\htdocs\assembly\uploader.php on line 37

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\phpBAE5.tmp' to '/uploadsexample.png' in C:\xampp\htdocs\assembly\uploader.php on line 37
Unable to save file.

If you could help me solve my issue I would be very grateful! thanks.

0

1 Answer 1

3

Add a trailing slash / for

define("UPLOAD_DIR", "/uploads");
                              ^ missing slash

change it to:

define("UPLOAD_DIR", "/uploads/");

Notice the error you got => to '/uploadsexample.png'

and in

Warning: move_uploaded_file(/uploadsexample.png):
                                    ^

it's missing a slash after uploads, between uploads and the filename.

Plus, just to be on the safe side, if that still throws you an error that it won't let you upload with another "permission denied" message, make sure that the folder is indeed writeable with proper write permissions set for it.

N.B.:

You may need to modify /uploads/ to uploads/ or ../uploads/ depending on the script's execution location.

Sign up to request clarification or add additional context in comments.

9 Comments

This is correct, the "permissions denied" is because it's confused due to a lack of "/"
Warning: move_uploaded_file(/uploads/example.png): failed to open stream: No such file or directory in C:\xampp\htdocs\assembly\uploader.php on line 37 Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php340B.tmp' to '/uploads/example.png' in C:\xampp\htdocs\assembly\uploader.php on line 37 Unable to save file. Is what i get now. The folder is writeable with permissions.
@user3317455 Is the folder writeable? Make sure it's 0755.
@Fred -ii- Alright, I created a new folder called uploads and made it writeable and that seemed to do it. Now i get a error like this , Notice: exif_imagetype(): Read error! in C:\xampp\htdocs\assembly\uploader.php on line 17 Unable to save file.
@user3317455 Ok. I think I know what's wrong. You may need to modify /uploads/ to uploads/ or ../uploads/ depending on the script's execution location.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.