这是我的代码
if ($error > 0)
{
die("Error uploading file! Code $error.");
}
else
{
if ($type === "image/png" || $type==="image/jpeg" && $size < 122200000)
{
$am = move_uploaded_file($temp,"$uploaded/$name");
}
else
{
die("Format not allowed or file size too big!");
}
}
此代码完美无缺。但是,我为用户提供了上传图像的选项。如果他们不想上传图片,那么我不希望这个代码运行。然后我重写了以下代码:
if(file_exists($_FILES["image"]))
{
if ($error > 0)
{
die("Error uploading file! Code $error.");
}
else
{
if ($type === "image/png" || $type==="image/jpeg" && $size < 122200000)
{
$am = move_uploaded_file($temp,"$uploaded/$name");
}
else
{
die("Format not allowed or file size too big!");
}
}
}
添加此内容后,如果未输入文件但未输入文件,则此代码不会运行,但不再上传到我的目录。奇怪的。我尝试使用 isset($ _ FILES [“image]),但这根本不起作用。有什么想法吗?
答案 0 :(得分:0)
尝试;
if (!empty($_FILES['image']))
{
// your code to handle update.
}
注意:强>
此外,你可以添加check in javascript,不允许清空文件的SUBMIT按钮(即没有选择文件。)
还有
如果(file_exists($ _ FILES [&#34;图像&#34;]))
您正在检查该文件是否存在于服务器上。
答案 1 :(得分:0)
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
echo "Displaying contents\n";
readfile($_FILES['userfile']['tmp_name']);
} else {
echo "File is not uploaded ";
echo "Or possible file upload attack: ";
echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
}