简体   繁体   English

将文件上传到服务器问题

[英]Uploading file to server problems

I'm trying to upload and save file in dir called images through a form. 我正在尝试通过表单上载并保存在名为images目录中的文件。 Form looks like this: 表单如下所示:

<form name="spremi" action="spremaj.php" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="image" />
<input type="submit" value="Send" name="send" />
</form>

And php script looks like this: php脚本看起来像这样:

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

Script always prints message "already exists". 脚本总是打印消息“已经存在”。 I know that questions like this were already asked, but none of the answers helped me. 我知道已经问过这样的问题,但是没有一个答案对我有帮助。 This code didn't work either on localhost, or web server. 此代码在localhost或Web服务器上均无效。 Thank you. 谢谢。

In your html form you do not have a file parameter so your php needs to accept image so change 在您的html表单中,您没有file参数,因此您的php需要接受image因此请进行更改

if (file_exists("images/" . $_FILES["image"]["name"]))
{
   echo $_FILES["image"]["name"] . " already exists. ";
}else
  move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);

to

if (file_exists("images/" . $_FILES["image"]["name"]))
{
   echo $_FILES["image"]["name"] . " already exists. ";
}else
   move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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