2

I'm working on a simple php script that allow me to upload images. File uploads correctly, but when I go to open the image uploaded in the directory there's an errore "you're not authorized to see this file, check the authorization and retry"

    <!DOCTYPE html>
<html>
    <head>
        <title> File upload </title>
        <meta charset = "UTF-8" />
    </head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Seleziona il file:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
</body>
</html>

    <?php
    $target_dir = "C:\Users\test\Desktop\upload_succeeded\\";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $fileType = pathinfo($target_file,PATHINFO_EXTENSION);

    // Controllo se il file esiste gia
    if (file_exists($target_file)) 
    {
        echo "Spiacenti, il file esiste gia'.";
        $uploadOk = 0;
    }

    // Abilitare solo alcuni formati di file
    if($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "gif" ) 
    {
        echo "Spiacenti, solo file JPG, JPEG, PNG & GIF sono abilitati.";
        $uploadOk = 0;
    }

    // Controllo se $uploadOk e' settato a 0 da un errore
    if ($uploadOk == 0) 
    {
        echo "Spiacenti, il tuo file non e' stato caricato.";
    // Se tutto e' ok prova a caricare il file
    } 
    else 
    {
        if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
        {
            echo "Il file ". basename( $_FILES["fileToUpload"]["name"]). " e' stato correttamente caricato.";
        } 
        else 
        {
            echo "Spiacenti, c'e' stato un errore nel caricamento del file.";
        }
    }
?>
KolteStar371
  • 73
  • 1
  • 8
  • 1
    It is a little less information you provide here, but I think you have another user for you web-server than for your desktop-folder... It is usually better to work with relative Paths and constants like "__DIR__". – Oliver Feb 13 '17 at 15:47

3 Answers3

2

I think I know the answer, but let me first explain what exactly happens here.

The browser sends a multipart/form-data through post that contains the file. Apache receives said file and PHP then places it in a temp directory. Where it is created as a new file on the file system. What you get through PHP is; the normalised checked file inside the $_FILES variable which goes through some further testing before it gets copied by the move_uploaded_file function.

What most likely is your problem is: that the user used by PHP/Apache and the permissions set while writing the file, does not allow the user you are current logged in with to open it.

2 Possible solutions:

Change the Apache / PHP user to write the file with permissions so you have rights to open it.

Or change permissions on the file afterwards, assuming the user used to created the file has enough permissions to do so:

Change permissions of file uploaded by PHP

Community
  • 1
  • 1
RC NL
  • 88
  • 4
0

You're running windows. Download any FTP client and connect to your server than change permission to files to 777(rwe).

F7eak
  • 24
  • 1
  • 1
    0777 permissions are not a good idea. And on a local mashine a ftp-client is not needed. And if you don't change the permissions of the parent folder, only of the files, you will never get to it. – Oliver Feb 13 '17 at 15:44
0

Instead of giving permission manually you can give permission in php using chmod() function as you upload any file right after uploading the file you can call this function with file_path/file_name and then can give permission accordingly use 0777 for all read/right permissions

chmod(IMAGES_DIR.'/'.'image_name.'.$image_file_type, 0777);
Awais fiaz
  • 351
  • 1
  • 5
  • 20