我正在寻找PHP文件上传方面的帮助。我正在尝试上传图像,使用以下代码(在w3schools教程中提供):
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
显然我对他们的代码进行了一些更改,但它并没有按照它所说的那样做。所以我问:
为什么这不会像它声称的那样创建一个名为'upload'的新文件夹?我收到以下错误:
Warning: move_uploaded_file(upload/donkeykong.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in ... etc.
我应该如何编写我想要上传图片的网址?
答案 0 :(得分:3)
它不会创建文件夹,因为永远不会调用mkdir()
。该代码假定upload/
目录已存在。如果目录不存在,则创建目录:
if (!file_exists('upload')) {
mkdir('./upload');
}
顺便说一下,开头的陈述可以通过多种方式清理。一种可能性是使用in_array()
而不是一堆||
条件:
if (in_array($_FILES['file']['type'], array("image/gif", "image/jpeg", "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000)
)
要将上传文件放在相对于服务器文档根目录的upload/
中,您可以使用$_SERVER['DOCUMENT_ROOT']
。但是,您应手动创建upload/
目录并使其可由Web服务器写入。为了通过PHP创建目录,整个文档根目录需要由Web服务器写入,这是一个巨大的安全漏洞。
$uploads_dir = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
move_uploaded_file($_FILES["file"]["tmp_name"], $uploads_dir . $_FILES["file"]["name"]);
答案 1 :(得分:2)
我之前构建过这个版本,适用于 JPEG , JPG , GIF , PNG
保存到当前目录“images”。
<?
max_size = 300; //size in kbs
if($_POST['Submit'] == "Upload"){$image =$_FILES["file"]["name"];$uploadedfile = $_FILES['file']['tmp_name'];
if ($image){$filename = stripslashes($_FILES['file']['name']);$extension = getExtension($filename); $extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {$change='Invalid Picture';$errors=1;}
else{$size=filesize($_FILES['file']['tmp_name']);
if ($size > $max_size*1024){$change='File too big!';$errors=1;}
else{
if($extension=="jpg" || $extension=="jpeg" ){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefromjpeg($uploadedfile);}
else if($extension=="png"){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefrompng($uploadedfile);}
else {$src = imagecreatefromgif($uploadedfile);}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
//MAIN IMAGE
$newwidth=300;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$kek=imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp,0,0,$kek);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
//Does Directory Exhist?
if(is_dir("images")==FALSE){mkdir("images");}
//Build file path and SAVE
$filepath = "images/".md5(genRandomString().$_FILES['file']['name']).".".$extension;
imagejpeg($tmp,$filepath,100);
imagejpeg($tmp,$filepath,100);
imagedestroy($src);
imagedestroy($tmp);
//ERROR HANDLING
if($_FILES["file"]["size"]<=0){$errors=1;$change='No file';}
if($errors!=1){$change='Image Uploaded!';}
}
}
}
}
?>
对于您的错误:
<div><? echo $change ?></div>
答案 2 :(得分:1)
1.通常意味着文件路径不正确或权限设置不正确。您应该创建上载目录并进行设置,以便相应的人员可以手动读取和写入。在apache中我认为它是这样的:
mkdir uploaded_files
chown -R nobody uploaded_files
chmod 755 -R uploaded_files
2.关于路径,你可以使用绝对或相对最好的方法来使用:
$_SERVER['DOCUMENT_ROOT] . '/uploaded_files'
答案 3 :(得分:1)
希望这有帮助
<?php
if (isset($_FILES['photo']))
{
$mimetype = mime_content_type($_FILES['photo']['tmp_name']);
if(in_array($mimetype, array('image/jpeg', 'image/gif', 'image/png'))) {
move_uploaded_file($_FILES['photo']['tmp_name'],
'/images/' . $_FILES['photo']['name']);
echo 'OK';
} else {
echo 'Not an image file!';
}
}