2

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.";
    }
}
?>
Luigi
  • 151
  • 1
  • 11

3 Answers3

1

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);
}
?>
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • Thank you very much Sulthan Allaudeen!! Your solution work perfectly!! (I can't vote up now because I don't have enough "reputation point" but i'll do!!) Is it possible to improve this code for the upload of txt and pdf too? Thanks again – Luigi May 31 '15 at 13:32
  • 1
    With the current code itself we can upload the .txt and .pdf files too :0 – Sulthan Allaudeen May 31 '15 at 13:34
  • thank you!! Is there any security problem letting someone upload files? – Luigi May 31 '15 at 13:35
  • If anyone knows the path they can able to upload files, If you want to restrict them, you can do it in two way.. 1. To have login system or 2. Just to have password for the particular file in simple way .. If you need to achieve it convey me, Its simple only :) – Sulthan Allaudeen May 31 '15 at 13:36
  • I was thinking to add the upload page in a passord protected php page of the site, is it enough? Is it possible to automatically add the uploaded images or txt in a page of the site? – Luigi May 31 '15 at 13:50
  • Do you mean.. U want to list the uploaded files list a php page ? – Sulthan Allaudeen May 31 '15 at 13:52
  • mm.. I would like that when I upload an image, this image is automatically visible in a page of the site. Eg, if I have a site of a music band, if I upload from the password protected page the poster of the next concert, and automatically in the page "events" i can see that poster (is maybe better if I made a new question for this?) – Luigi May 31 '15 at 14:11
  • Yeah, sure. You post a new question and ping me ;) – Sulthan Allaudeen May 31 '15 at 14:11
0

As you suggested in your post, try to change the permission of the folder following the code below:

chmod("/tmp/uploads", 0777);
Llogari Casas
  • 942
  • 1
  • 13
  • 35
  • Thank you very much for the quick answer, I tried adding chmod("/tmp/uploads", 0777); at the end of the upload.php, but it doesn't work.. – Luigi May 31 '15 at 11:43
  • Chmod function should be added at the beginning of your php file @Luigi – Llogari Casas May 31 '15 at 11:45
  • Sorry, I'm really not very able with php, I tried to put also at the beginning but it doesn't work again. I put it after – Luigi May 31 '15 at 11:52
  • Do you get some error info when running your PHP that can help us to know what the problem is? @Luigi – Llogari Casas May 31 '15 at 11:54
  • the only thing I get when trying to upload an image is the message: "File is an image - image/jpeg.Sorry, there was an error uploading your file." @Llogari Casas – Luigi May 31 '15 at 11:56
  • Okay, then the error is defenitly a result of permission issues. The line that is debbuging error to you is this: `if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {` Which is returning `false` due the fact that is was impossible to move the uploaded file there. – Llogari Casas May 31 '15 at 12:02
  • 1
    Thank you, I'm happy to have found someone able to understand which the problem is! Is there any way to fix this issue? – Luigi May 31 '15 at 12:12
  • The only way to fix the problem is to change the permissions of the folder. You can do it either trough `PHP` following my answer or via `FTP client` if you are using one @Luigi – Llogari Casas May 31 '15 at 12:16
  • Sorry again for the disturb, I tried as you told me with php: mine php now is the same of the question except the first rows: – Luigi May 31 '15 at 12:24
  • What about setting a dropbox folder for the upload? I added a new question thanks stackoverflow.com/q/30675159/4842333 – Luigi Jun 05 '15 at 22:06
0

you are using absolute path linux type, /tmp/uploads, if you are using linux try to show permission folder with "ls -l /tmp/uploads", if you are using windows hosts maybe you can print the path $target_file, inthe w3schools example the tagtet_path doesnt have the "/" "tmp/uploads" != "/tmp/uploads".