Uploading file via PHP, doesn't work

2019-02-13 20:10发布

I copied the code from this page: http://www.w3schools.com/php/php_file_upload.asp

Problem is that i don't get an error when uploading a file, instead this shows which should be correct:

Upload: images.jpeg Type: image/jpeg Size: 5.8603515625 kB Temp file: /tmp/phpZ67YXk Stored in: upload/images.jpeg

But no file is saved on the server.

I don't know what is wrong but I'm thinking in terms of permission, still there is a folder named upload with 777 permissions.

These php-files are hosted on a online web host so I don't run this locally.

HTML-form

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

upload_file.php

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

2条回答
Rolldiameter
2楼-- · 2019-02-13 20:42

I suppose the problem is in your relative path of destination (relative according to your current working directory = path of your .php file), try to make it absolutem like this:

move_uploaded_file(
  $_FILES["file"]["tmp_name"],
  $_SERVER['DOCUMENT_ROOT'] . 'upload/' . $_FILES["file"]["name"]
);
查看更多
放我归山
3楼-- · 2019-02-13 20:47

this help you:

chmod 777 foldername

php code:

<?php
if(isset($_FILES['image'])){
  $errors= array();
  $file_name = $_FILES['image']['name'];
  $file_size =$_FILES['image']['size'];
  $file_tmp =$_FILES['image']['tmp_name'];
  $file_type=$_FILES['image']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
  $expensions= array("jpeg","jpg","png", "mp3",
                     "acc", "wav", "3gpp", "mp4", "3gp", "m4a", "amr", "avi",
                    "flv", "gif");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  }

  if($file_size > 2097152666655){
     $errors[]='File size must be excately 2 MB';
  }

  if(empty($errors)==true){
     move_uploaded_file($file_tmp,"/var/www/upload/users/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }
}
?>

html:

<html>
<body>

  <form action="" method="POST" enctype="multipart/form-data">
     <input type="file" name="image" />
     <input type="submit"/>
  </form>

</body>
</html>
查看更多
登录 后发表回答