通过 url 在 php 中上传图像

发布于 2024-11-05 17:06:17 字数 1635 浏览 1 评论 0原文

我有这个表单来上传图像:

<form method="post" action="upld.php" name="insertForm"  enctype="multipart/form-data">
Image name:<br />
<input type="text" name="iname" /><br />
<input type="file" name="file" />
<input type="submit" name="upload" value="Upload" />

</form>

这里是upld.php,

<?php
$db_name = "DB_name";
$table_name = "tble";
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());

if(isset($_POST['upload'])){

 if  (($_FILES["file"]["error"] > 0))
    {
echo "<h3> Error in File! </h3>";
    }
  else
    {
    if ((file_exists("images/" . $_FILES["file"]["name"])) )
      {
      echo "<h3> file not exsists!</h3>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/" . $_FILES["file"]["name"]);

//$id=mysql_insert_id();
$time=strftime("%Y-%m-%d %H:%M:%S", time());
$img_name=$_POST['iname'];
$img=$_FILES["file"]["name"];
$sql="INSERT INTO $table_name VALUES('{$img}','{$time}','{$img_name}')";
$result=mysql_query($sql,$connection);
mysql_close($connection);
echo "<h3>uploaded successfully</h3>";
}
}
}
echo "<br><br><a href='GalleryAdmin.php'>GO back to Admin Gallery</a>
";
?> 

问题是:

当我运行它时总是说我的文件不存在,据此如果

 if ((file_exists("images/" . $_FILES["file"]["name"])) )
      {
      echo "<h3> file not exsists!</h3>";

我的图像文件夹与upld.php位于同一文件夹中,

你会怎么做猜猜是问题所在?

i have this form to upload images:

<form method="post" action="upld.php" name="insertForm"  enctype="multipart/form-data">
Image name:<br />
<input type="text" name="iname" /><br />
<input type="file" name="file" />
<input type="submit" name="upload" value="Upload" />

</form>

and here is the upld.php

<?php
$db_name = "DB_name";
$table_name = "tble";
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());

if(isset($_POST['upload'])){

 if  (($_FILES["file"]["error"] > 0))
    {
echo "<h3> Error in File! </h3>";
    }
  else
    {
    if ((file_exists("images/" . $_FILES["file"]["name"])) )
      {
      echo "<h3> file not exsists!</h3>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/" . $_FILES["file"]["name"]);

//$id=mysql_insert_id();
$time=strftime("%Y-%m-%d %H:%M:%S", time());
$img_name=$_POST['iname'];
$img=$_FILES["file"]["name"];
$sql="INSERT INTO $table_name VALUES('{$img}','{$time}','{$img_name}')";
$result=mysql_query($sql,$connection);
mysql_close($connection);
echo "<h3>uploaded successfully</h3>";
}
}
}
echo "<br><br><a href='GalleryAdmin.php'>GO back to Admin Gallery</a>
";
?> 

the problem is:

when i run it always say me file not exsist, acording to this if

 if ((file_exists("images/" . $_FILES["file"]["name"])) )
      {
      echo "<h3> file not exsists!</h3>";

i have the images folder with upld.php in the same folder

what would you guess is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晨与橙与城 2024-11-12 17:06:17

我认为你有一个轻微的逻辑错误

file_exists("images/" . $_FILES["file"]["name"])

如果文件存在于图像文件夹中则会返回 true (我猜如果有人已经上传了它)。但是,根据您的日志声明,您想要的是

!file_exists("images/" . $_FILES["file"]["name"])

I think you have a slight logical error

file_exists("images/" . $_FILES["file"]["name"])

Will return true if the file exists in the images folder (my guess would me if somebody already uploaded it). But, based on your log statement, what you want is

!file_exists("images/" . $_FILES["file"]["name"])
夏至、离别 2024-11-12 17:06:17

好的,您已上传文件。但是,您检查的是它是否在“images/my.jpg”中。此时它位于 tmp_name 中,在您的 tmp 目录中,很可能是这样,不,此时它始终不存在,因为该文件仅以临时名称存在,您需要在临时位置中检查它,移动它,然后检查它是否确实在图像中?

OK so you uploaded your file. But, what you checked was if it was in say "images/my.jpg". At this point its in tmp_name, in your tmp directory, most likely so, no it would always not exist at this point as the file is only in a temporary name, you need to check its in your temp location, move it, and then check if its in images surely?

一指流沙 2024-11-12 17:06:17

首先:

PHP 将文件上传到临时目录。这是您需要移动到 images/ 文件夹的文件。您可以在服务器上的以下位置找到该文件:

$_FILES['file']['tmp_name']

这是您要对其运行 file_exists 以确保上传成功完成的文件。所以:

if (file_exists($_FILES['file']['tmp_name']) {
  // File upload successful. Now move file to your directory.
  move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
  // Now do the database stuff here.
  // ... 
} else {
  // Nothing was uploaded and something is wrong!
}

次要:

您的代码

file_exists("images/" . $_FILES["file"]["name"])

将返回 TRUE,因此(在您的代码中)它会说没有文件。这是你的逻辑错误。

尝试:

!file_exists("images/" . $_FILES["file"]["name"])

相反。

第三:

确保您将文件移动到的目标文件 (images/) 具有正确的 chmod。它需要 775 才能创建文件。这是通过 ftp 程序完成的。

在此处阅读更多信息:CHMOD 教程

您还需要将文件从tmp 目录到图像,然后检查它是否存在于 file_exists 中。

First:

PHP uploads the file to a temp directory. This is the file you need to move to your images/ folder. You find the file in this location on your server:

$_FILES['file']['tmp_name']

This is the file on which you want to run file_exists to make sure the upload completed successfully. So:

if (file_exists($_FILES['file']['tmp_name']) {
  // File upload successful. Now move file to your directory.
  move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
  // Now do the database stuff here.
  // ... 
} else {
  // Nothing was uploaded and something is wrong!
}

Secondary:

Your code

file_exists("images/" . $_FILES["file"]["name"])

will return TRUE, and therefore (in your code) it will say that there are no file. That's a logical error on your part.

Try:

!file_exists("images/" . $_FILES["file"]["name"])

instead.

Third:

Make sure that the file to which you move the file (images/) have the proper chmod. It needs 775 for it to be possible to create files into. This is made via the ftp program.

Read more here: CHMOD tutorial

You will also need to move the file from the tmp dir to images before checking if it's there with file_exists.

一抹苦笑 2024-11-12 17:06:17

请在检查文件是否存在之前使用 move_uploaded_file;)

否则请尝试以下操作:

1.) error_reporting(E_ALL);

2.) chmod 图像目录 (775),右键单击目录

Please use move_uploaded_file before you check if the file exists;)

Otherwise try this:

1.) error_reporting(E_ALL);

2.) chmod the images directory (775), right mouse click on directory

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文