简体   繁体   中英

how to upload file using php

plz help me I dont know how to make this work

<form action="accept-file.php" method="post" enctype="multipart/form-data">
    Your Photo: <input type="file" name="photo" size="25" />
    <input type="submit" name="submit" value="Submit" />
</form>

the accept-file.php is

if ($_FILES["file"]["error"] > 0) {
  echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
}

I think you are missing something like this:

  move_uploaded_file($_FILES["file"]["tmp_name"],
  "../img/profiles/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "../img/profiles/" . $_FILES["file"]["name"];

Can u try it?

$path = $_FILES['file']['name'];
if (file_exists($path)) {
    echo 'File already exists!';
} else {
    move_uploaded_file($_FILES['file']['tmp_name'], $path);
}

use move_uploaded_file(file,location)

you need to change your file field name to file instead photo

<input type="file" name="file" size="25" />

if don't change than use code like below to upload it.

move_uploaded_file($_FILES["photo"]["tmp_name"], "location/to/save/photo/with/extension");
<input type="file" name="photo" size="25" />

should be like

<input type="file" name="file" size="25" />

because you are looking for $_FILES["file"] not $_FILES["photo"];

make change to either your input name or the $_FILE[] as you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM