上传照片 PHP
最奇怪的事情发生了...我用 PHP 创建了一个网上商店。一切工作正常,但是当我们将网站从我们的 ftp 转移到他们的 ftp 时,我们遇到了问题。我们无法再上传图片了...
if (file_exists("../productimages/" . $_FILES["picture"]["name"]))
{
$feedback = "<div class=\"voegproducttoefeedback\">Please change name of product<b><big> \"".$_FILES["picture"]["name"] . "\"
</big></b>The name already occurs in the database</div> ";
} else
{
$tmp_name = $_FILES["picture"]["tmp_name"];
$name = $_FILES["picture"]["name"];
move_uploaded_file($tmp_name, "../productimages/$name");
if(file_exists("../productimages/$name")) {
$feedback2 = "succeed";
}
else {
$feedback2 = "failed";
}
if ($product->saveproduct($_DB)) {
$feedback = "<div class=\"voegproducttoefeedback\">Product <b><big>\"".$product->naam."\"</big></b> created with succes</div>";
} else {
$feedback = "<div class=\"voegproducttoefeedback\">Not enough information to create product</div>";
}
}}}?>
strangest thing happened... I created a webshop in PHP. Everything worked fine, but when we moved the website from our ftp to theirs, we ran into a problem. We aren't able to upload pictures anymore...
if (file_exists("../productimages/" . $_FILES["picture"]["name"]))
{
$feedback = "<div class=\"voegproducttoefeedback\">Please change name of product<b><big> \"".$_FILES["picture"]["name"] . "\"
</big></b>The name already occurs in the database</div> ";
} else
{
$tmp_name = $_FILES["picture"]["tmp_name"];
$name = $_FILES["picture"]["name"];
move_uploaded_file($tmp_name, "../productimages/$name");
if(file_exists("../productimages/$name")) {
$feedback2 = "succeed";
}
else {
$feedback2 = "failed";
}
if ($product->saveproduct($_DB)) {
$feedback = "<div class=\"voegproducttoefeedback\">Product <b><big>\"".$product->naam."\"</big></b> created with succes</div>";
} else {
$feedback = "<div class=\"voegproducttoefeedback\">Not enough information to create product</div>";
}
}}}?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在你自己的答案中,你说:
解决此问题的最佳方法是将目录的所有权更改为网络服务器运行的用户。但是,这将需要对服务器的 root 访问权限(或 sudo 命令),而您很可能没有该权限。因此,这里有一个解决方法:
重写脚本以检查
productimages
是否存在,如果不存在,则创建它:if(!is_dir('../productimages/')) mkdir('../productimages/', 0755);
然后,删除
productimages
目录。您将让脚本为您重新创建它。完成后,网络服务器的用户将拥有该脚本。为了实现这一点,
chmod 777
*父目录。 这只是临时的,并允许脚本创建目录。运行脚本。确保它创建了
productimages
目录。如果是这样,chmod 755
返回父目录。这应该使您能够获得所需的权限并控制目录的访问。
In your own answer, you said:
The best way to resolve this is to change the ownership of the directory to the user the webserver runs as. However, this will require root access to the server (or the
sudo
command), which you most likely don't have. So here's a workaround:Rewrite your script to check if
productimages
exists and, if not, create it:if(!is_dir('../productimages/')) mkdir('../productimages/', 0755);
Then, delete the
productimages
directory. You're going to let the script re-create it for you. When it does, the webserver's user will own the script.In order to make this happen,
chmod 777
*the parent directory. This is only temporary, and allows the script to create the directory.Run the script. Make sure it created the
productimages
directory. If it did,chmod 755
back the parent directoy.This should enable you to get the permissions you need as well as have control over the access of the directory.
尝试更改目标目录权限(../productimages/)
Try changing the target directory permissions (../productimages/)
正如 Goo 所说,检查权限,但还要确保 PHP 配置为允许上传,并且最大上传大小设置正确。
检查 file_uploads 设置为“on”并且 upload_max_filesize 设置为合理的限制。
As Goo says, check permissions, but also ensure that PHP is configured to allow uploads and that the maximum upload size is set correctly.
Check file_uploads is set to 'on' and upload_max_filesize is set to a sensible limit.
如果您有权访问它们,请检查您的 PHP 错误日志。
您的主机很可能没有为负责 PHP 进程的帐户启用写入权限。您需要看看他们是否可以启用该功能。
If you have access to them, check your PHP error logs.
Your host most likely hasn't enabled write permissions for the account in charge of the PHP process. You'll need to see if they can enable that.
好的,问题(部分)解决了。我更改了productimages的权限...但是该过程的成功需要777权限...虽然不是很安全。这是我的结构,也许你可以看到问题...
上面的脚本位于 products.php
http://img695.imageshack.us/i/schemafbeelding2011010kt.png/
Ok, problem (partial) solved. I changed the permissions of productimages... But the success of the process required a 777 permission... Not very secure though. Here is my structure, maybe you can see the problem...
The above script is in products.php
http://img695.imageshack.us/i/schermafbeelding2011010kt.png/