Php 移动图片时遇到问题,移动文件()已上载
我正在尝试为我正在创建的网页设置一些图像处理,但我无法使move_Upload_文件正常工作。。。我不断地发现这些错误:Php 移动图片时遇到问题,移动文件()已上载,php,Php,我正在尝试为我正在创建的网页设置一些图像处理,但我无法使move_Upload_文件正常工作。。。我不断地发现这些错误: Warning: move_uploaded_file(/htdocs/PHP/Pictures/picture.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /opt/lampp/htdocs/PHP/useredit.php on line 1
Warning: move_uploaded_file(/htdocs/PHP/Pictures/picture.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /opt/lampp/htdocs/PHP/useredit.php on line 17
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpY0KKxH' to '/htdocs/PHP/Pictures/picture.jpg' in /opt/lampp/htdocs/PHP/useredit.php on line 17
我的代码如下所示:
if(isset($_FILES['image_file']))
{
$img_tmp_name = $_FILES['image_file']['name'];
$img_dir = "/htdocs/PHP/Pictures/";
$img_name = $img_dir . $img_tmp_name;
if(move_uploaded_file($_FILES['image_file']['tmp_name'],$img_name))
{
list($width,$height,$type,$attr) = getimagesize($img_name);
switch($type)
{
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Image format not accepted";
}
$query = "UPDATE profile_pic SET img_path=$img_name WHERE uid='$uid'";
$img_id = mysql_insert_id();
$new_img_name = $img_dir . $img_id . $ext;
rename($img_name, $new_img_name);
}
}
if(mysql_query($query)or die('Error: ' . mysql_error()))
{
header("Refresh:0; url='control.php'");
}
存在PHP/Pictures文件夹。如何解决此问题?此代码存在一些重大的安全和后勤问题: a您不会检查上载是否成功,并继续进行,就好像它成功了一样。上传成功的方法只有一种,失败的原因太多了
if ($_FILES['image_file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code {$_FILES['image_file']['error']}");
}
b您正在使用用户在路径中提供的文件名保存到服务器上。恶意用户可以在该文件名中嵌入路径数据,并在您的服务器上指定他们想要的任何位置。e、 g
$_FILES['image_file']['name'] = '../../../../../../etc/passwd';
$img_dir应该包含相对于当前文件的路径,而不是根文件夹的路径 如果当前目录包含upload_file.php ur代码和文件夹层次结构,如php/Pictures/
然后$img_dir=/PHP/Pictures/ 我怀疑htdocs是您服务器上的根级别文件夹。将该路径更改为相对于此脚本的目录/htdocs应该在根级别,因此,如果lampp/同时包含tmp/和htdocs/我应该将目录更改为../htdocs/PHP/Pictures?感谢您为我提供上传错误的提示,我包含了您的代码。至于文件名安全风险,我很清楚这一点,我会尽快解决,但我喜欢一次只做一件事。另外,我现在只是在一个本地服务器上。