上传图片失败 php
Upload image failing in php
我正在尝试使用 php 上传文件。但是,由于某些奇怪的原因,即使只有两条路径,我也没有从 echo 得到任何反馈。
<?php
$allowedExts = array("jpg");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (
($_FILES["file"]["type"] == "image/jpg")
&& ($_FILES["file"]["size"] < 100000)
&& in_array($extension, $allowedExts))
{
echo "Storing";
$temp = explode(".",$_FILES["file"]["name"]);
$newfilename = "yo.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $newfilename;
echo "Stored";
}
else
{
echo "Invalid file";
}
?>
我在 3 小时后弄明白了。
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
$uploadOk = 2;
}
if($imageFileType != "jpg" && $imageFileType != "jpeg") {
$uploadOk = 0;
}
if ($uploadOk == 0) {
//file is not the proper image format
}
else if ($uploadOk == 2) {
//too big
}
else {
$target_file = $target_dir . 'goofy' .'.jpg';
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
我正在尝试使用 php 上传文件。但是,由于某些奇怪的原因,即使只有两条路径,我也没有从 echo 得到任何反馈。
<?php
$allowedExts = array("jpg");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (
($_FILES["file"]["type"] == "image/jpg")
&& ($_FILES["file"]["size"] < 100000)
&& in_array($extension, $allowedExts))
{
echo "Storing";
$temp = explode(".",$_FILES["file"]["name"]);
$newfilename = "yo.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $newfilename;
echo "Stored";
}
else
{
echo "Invalid file";
}
?>
我在 3 小时后弄明白了。
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
$uploadOk = 2;
}
if($imageFileType != "jpg" && $imageFileType != "jpeg") {
$uploadOk = 0;
}
if ($uploadOk == 0) {
//file is not the proper image format
}
else if ($uploadOk == 2) {
//too big
}
else {
$target_file = $target_dir . 'goofy' .'.jpg';
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}