简体   繁体   English

php文件上传问题

[英]php file upload problem

This code works properly in my localhost. 此代码在我的本地主机中正常工作。 I am using xampp 1.7.3. 我正在使用xampp 1.7.3。 but when I put it in the live server it shows Possible file upload attack! 但是当我将其放在实时服务器中时,它显示Possible file upload attack! . 'upload/' is the folder under 'public_html' folder on the server. “ upload /”是服务器上“ public_html”文件夹下的文件夹。 I can upload files via other script in that directory. 我可以通过该目录中的其他脚本上传文件。

<?php

$uploaddir = '/upload/';//I used C:/xampp/htdocs/upload/ in localhost. is it correct here?
$uploadfile = $uploaddir . basename($_FILES['file_0']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['file_0']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\\n";
} else {
    echo "Possible file upload attack!\\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

You probably can't move your file to /upload/ which is an "upload" folder at the root of the server file system, hence move_uploaded_file() reporting FALSE and your message. 你可能无法将您的文件/upload/这是在服务器的文件系统的根目录中的“上传”文件夹,因此move_uploaded_file()报告FALSE和您的邮件。 Plus, this /upload/ folder probably doesn't even exist nor is it writeable. 另外,此/upload/文件夹可能甚至不存在,也不可写。

You probably want to move it to $_SERVER['DOCUMENT_ROOT'].'/upload/' which will point to your virtual host root (something like www or wherever you're uploading your application files). 您可能希望将其移动到$_SERVER['DOCUMENT_ROOT'].'/upload/' ,它指向您的虚拟主机根目录(例如www或您要上传应用程序文件的任何位置)。 Don't forget to create this folder and to change its permissions accordingly (CHMOD 777 is a good idea). 不要忘记创建此文件夹并相应地更改其权限(CHMOD 777是个好主意)。

The problem is the leading slash in your file name. 问题在于文件名中的斜杠。 While it might resolve correctly on your XAMPP machine when it's on your server box the leading slash will try to put it in the filesystem root. 当XAMPP机器在服务器上时,它可能在您的XAMPP机器上正确解析,但斜杠会尝试将其放置在文件系统根目录中。

It's guessing that it's an attack because people can sometimes fudge incoming parameters to drop harmful files where they can execute them! 猜测这是一种攻击,因为人们有时可能会捏造传入的参数,以将有害文件拖放到可以执行它们的地方!

Most likely the $uploaddir is wrong. $ uploaddir很可能是错误的。 Use 采用

echo dirname(__FILE__);

to get the real full path to your root folder on the web server and then put something like 获取Web服务器上根文件夹的真实完整路径,然后输入类似

/web/real/path/to/root/upload

as the path. 作为路径。

Try this code 试试这个代码

 $fltype=$_FILES['userfile']['type']; 
    echo $fltype."<br>";
    /*if($_FILES['userfile']['type'] != "image/gif") {
    echo "Sorry, we only allow uploading GIF images";
    exit;
    }*/

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$imgname=basename($_FILES['userfile']['name']);
$desc=$_POST["desc"];
echo "<br>".$imgname."<br>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "File uploading failed.\n";
}
$con=mysql_connect("localhost","root","");
$select=mysql_select_db("n1",$con);
$sql="insert into tblimage (imagename,description) values('$imgname','$desc')";
echo "<br>".$sql;
$rs=mysql_query($sql);
if($rs)
{
    echo "<br>Record inserted in table.<br>";
}
else
{
echo "<br>Error in table insersion.<br>";
}
echo "<br><br>uploaded image is::::<br><br>";
echo "<img src='$uploadfile' />";
?> 

I had the same problem, uploading script worked on localhost but got Possible File Attack Error! 我遇到了同样的问题,上载脚本在localhost上有效,但是出现可能的文件攻击错误! on host server. 在主机服务器上。

I fixed it by giving 777 permission to upload folder. 我通过授予777上载文件夹的权限来修复它。 Goto ftp and right click on folder and choose properties. 转到ftp,然后右键单击文件夹并选择属性。 Tick all boxes (read/write/execute) and grant 777 permission. 勾选所有框(读/写/执行)并授予777权限。

Also edit php.ini file usually in etc/ folder on linux. 还可以在Linux的etc /文件夹中编辑php.ini文件。 Change the following settings to 将以下设置更改为

upload_max_filesize : 1024M post_max_size : 1024M max_execution_time : 6000 max_input_time : 6000 memory_limit : 128M upload_max_filesize:1024M post_max_size:1024M max_execution_time:6000 max_input_time:6000 memory_limit:128M

You can download putty to access linux server using SSH terminal. 您可以通过SSH终端下载腻子以访问linux服务器。

GOOD LUCK 祝好运

Why not just do this: 为什么不这样做:

$uploaddir = './upload';

it will be relative to where your script is, it that is the intent? 这将与您的脚本所在的位置有关,那是目的吗? Otherwise you need the full dir (from system root) 否则,您需要完整的目录(从系统根目录开始)

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

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