Search code examples
phpfile-permissions

PHP upload not working, maybe permission issues


I need to make an upload page in my site, I'm using an altervista trial server. I used the tutorial http://www.w3schools.com/php/php_file_upload.asp but the upload doesn't work. maybe, as I read is a permission issue, but I don't have the slightest idea on how to change the permissions of my folders.

Is it possible to add also pdf in the uploadable files?

thanks

uploadpage.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento senza titolo</title>
<link href="valsilehome.css" rel="stylesheet" type="text/css">
</head>

<body>


<form action="/tmp/upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

upload.php

<?php
$target_dir = "/tmp/uploads"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists 
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

Solution

  • As of w3 schools, they have created many conditions for the example. But we don't need follow it strictly. We can use it only if we needed.

    Here's the Minimal Code that you can have

    <!DOCTYPE html>
    <html>
    <body>
    <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    </body>
    </html>
    <?php
    if (isset($_POST['submit'])) 
    {
    echo 'succesfully uploaded';
    $structure = 'uploadedfiles/';
    $target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
    }
    ?>
    

    If you use the above code you should create a folder named as uploadedfiles in the folder where you keep this file.

    Else if you need to create each folder for each file upload then you should code.. It will create the file's name as folder each time.

    <!DOCTYPE html>
    <html>
    <body>
    <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    </body>
    </html>
    <?php
    if (isset($_POST['submit'])) 
    {
    echo 'succesfully uploaded';
    $structure = $_FILES["fileToUpload"]["name"];
    if (!mkdir($structure, 777, true)) 
    {}
    $target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
    }
    ?>