为什么我上传的文件不存在于我的文件系统中而且我的PHP命令没有返回?

为什么我上传的文件不存在于我的文件系统中而且我的PHP命令没有返回?

问题描述:

I'm having trouble creating a form that will upload and store images.

After uploading (submitting the form), I get this with the print_r($FILES) function:

Array ( [Image] => Array ( [name] => 4HfoEtn.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php151F.tmp [error] => 0 [size] => 70107 ) )

However, when navigating to C:\Windows\Temp\ in my file browser, the file does not exist.

Furthermore, when I run:

$tmp_image_dir = basename($_FILES['Image']['tmp_name']);
echo "<img src=\"" . $tmp_image_dir . "\">" . "<br>";

No image shows up.

Also, when I run:

$image_dir = "/images/";
$image_dir = $image_dir . basename($_FILES['Image']['name']);
if(move_uploaded_file($_FILES['Image']['tmp_name'], $image_dir)) echo "Image Uploaded Successfully" . "<br>";

I do not get a readout of "Image Uploaded Successfully"

Then, pretty obviously, when I run:

echo "<img src=\"" . $image_dir . "\">" . "<br>

I also get an image placeholder with no image.

Make sure $image_dir exists, if not create it with mkdir or manually.
You dont need to use basename in $_FILES["Image"]["name"] since its already the basename of file.

Try below code

<?php
  $image_dir  = "images/";
  if ( !file_exists( $image_dir ) ) {
    mkdir( $image_dir, 0755 );
  }
  $image_dir  = $image_dir.$_FILES["Image"]["name"];
  if ( move_uploaded_file( $_FILES["Image"]["tmp_name"], $image_dir ) ) {
    echo "Image Uploaded Successfully<br>";
  }
  echo '<img src="'.$image_dir.'">';
?>