0

I'm trying to upload a file in a server with PHP, but i have some problems. I have found this guide: http://www.sumedh.info/articles/store-upload-image-postgres-php-2.html. My html is:

<form action="img_user.php" method="POST" enctype="multipart/form-data" >
  <button id="buttonImgProf" class="btn" type="button" onclick="caricaImgProf()">Inserisci un immagine</button>

  <div id="imgProfLoader"  class="postContent" style="display:none;">
    Name : <input type="text" name="name" size="25" length="25" value="">
    <input type="file" name="userfile"></input>
    <button class="btn" type="submit">Carica immagine</button>
  </div>
</form>

(parts are not displays because i use javascript). The php code is:

$uploaddir = 'localhost'; //i have try lots of dir, maybe the error is here?

    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    $name = $_POST['name'];
    echo $_FILES['userfile']['tmp_name'];
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {    echo "File is valid, and was successfully uploaded.\n";
    }
    else   {   echo "File not uploaded";   }

the output is File not uploaded

Ranveer
  • 6,683
  • 8
  • 45
  • 92
  • please don't overwrite your question's code with the one you posted in answers. That should be an edit and marked as **EDIT:**.... what I tried". – Funk Forty Niner Dec 21 '16 at 15:20

3 Answers3

0

$uploaddir = 'localhost'; - localhost is for database connection purposes, not for a folder upload directive; use a folder that is writeable.

As per the manual http://php.net/manual/en/features.file-upload.post-method.php

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I have checked the php.ini file and it is : file_uploads = On. then i change my code in this way: – Diego Zucca Dec 21 '16 at 15:16
  • @DiegoZucca as I wrote in my answer, `$uploaddir = 'localhost';` the `localhost` is reserved to database syntax, and is not considered as a folder. Read the manual and the example I pulled from it and make sure the folder for it can be written to. – Funk Forty Niner Dec 21 '16 at 15:17
0

your upload path($uploaddir = 'localhost'; ) should be physical path not should be url so change localhost to any path like ($uploaddir = "uploads/")

full demo code :

First, ensure that PHP is configured to allow file uploads.

In your "php.ini" file, search for the file_uploads directive, and set it to On: file_uploads = On

then your form page

<!DOCTYPE html>
<html>
<body>

<form action="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>



Make sure that the form uses method="post"
The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form

The "upload.php" file contains the code for uploading a file:(it is action page for file upload form)

     <?php
        $target_dir = "uploads/";
    if (is_dir($upload_dir) && is_writable($upload_dir)) {
    // you can write anything in this folder 
} else {
    die('Upload directory is not writable, or does not exist.');
}
        $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 $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.";
            }
        }
        }
        ?>

PHP script explained:

$target_dir = "uploads/" - specifies the directory where the file is going to be placed
$target_file specifies the path of the file to be uploaded
$uploadOk=1 is not used yet (will be used later)
$imageFileType holds the file extension of the file
Next, check if the image file is an actual image or a fake image

Note: You will need to create a new directory called "uploads" in the directory where "upload.php" file resides. The uploaded files will be saved there.

Hassan Saeed
  • 6,326
  • 1
  • 39
  • 37
  • i copy this code and it returned : File is an image - image/jpeg.Sorry, there was an error uploading your file. – Diego Zucca Dec 21 '16 at 15:28
  • I have got a problem with move_uploaded_file() function – Diego Zucca Dec 21 '16 at 15:30
  • 1st make "uploads" folder where your current php pages placed and Check that the web server has permissions to write to the "uploads/" directory.just set the "uploads/" folder permissions to 777 – Hassan Saeed Dec 21 '16 at 15:34
  • Oh, sorry. I explain: i am using ubuntu, a system where for write the folder, for example /var/www etc (syst. folders), must be super user, at this point i think that this is the problem. how i can change servers permissions? – Diego Zucca Dec 21 '16 at 15:38
  • i changed some code which will proof that it is directory writable (is_writable) problem or you are giving wrong path which is not exit in your system – Hassan Saeed Dec 21 '16 at 15:40
  • if this is permission problem then use this command : sudo chown -R www-data:www-data /var/www – Hassan Saeed Dec 21 '16 at 15:44
  • OH! Was a permission problem! Now i have fixed "-R www-data:www-data /var/www" and: File is an image - image/jpeg.The file 10153093_759811030703965_6589385567291672804_n.jpg has been uploaded. Thank you so much!! – Diego Zucca Dec 21 '16 at 15:48
0

I put your HTML and PHP code in a single file and wrapped your PHP code with a "not empty" $_FILES and it seems to work fine. Here's the full file.

I have also removed the display:none; style you had put on your div to make it useable, since you did not provide the contents of your caricaImgProf() function. Oh and the $uploaddir cannot be a domain such as localhost, it must be a valid directory, so I removed your localhost reference, so that the file uploads right next to where the script is (same directory) which you shouldn't do in a production server of course, but it's off topic :P

This single file should be named img_user.php :

<?php

if(!empty($_FILES)){
    $uploadfile = basename($_FILES['userfile']['name']);
    $name = $_POST['name'];
    echo $_FILES['userfile']['tmp_name'];
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {    echo " / File is valid, and was successfully uploaded.\n";
    }
    else   {   echo "File not uploaded";   }
    exit();
}

?><html>
<body>
<form action="img_user.php" method="POST" enctype="multipart/form-data">
    <button id="buttonImgProf" class="btn" type="button" onclick="caricaImgProf()">Inserisci un immagine</button>
    <div id="imgProfLoader" class="postContent">
        Name : <input type="text" name="name" size="25" length="25" value="">
        <input type="file" name="userfile" />
        <button class="btn" type="submit">Carica immagine</button>
    </div>
</form>
</body>
</html>

It outputs this when I submit a picture file and the file correctly gets saved right next to the PHP script in the same folder with the original filename before the upload:

E:\wamp64\tmp\php38E6.tmp / File is valid, and was successfully uploaded.

Marc T.
  • 21
  • 7
  • So you think that the problem is because div is not shows so browser doesn't take any img data taken by thid div? – Diego Zucca Dec 21 '16 at 15:20
  • No. Please take my file and directly try it without modifying anything and tell me if it works, because if it does not, then it means you have a server configuration that needs to be changed. Let me know, thx. – Marc T. Dec 21 '16 at 15:24
  • Did you try it yet? It's your own script with very little modifications to make it work. You should try it! – Marc T. Dec 21 '16 at 15:47