多个 php 上传,每个文件具有不同的目标位置
multiple php upload with each file having a different target location
我正在尝试在我们的网站上创建一个上传,基本上我们可以将各种尺寸的图像添加到输入中,它们都会转到相应的目标位置。
这是表格
<form action="upload.php" method="post" enctype="multipart/form-data">
Select 120px image to upload:
<input type="file" name="img120" id="img120">
Select 125px image to upload:
<input type="file" name="img125" id="img125">
Select 600px image to upload:
<input type="file" name="img600" id="img600">
Select default image to upload:
<input type="file" name="imgdef" id="imgdef">
<input type="submit" value="Upload Images" name="submit">
</form>
这提交到一个名为upload.php的php文件,其代码如下:
// set upload target directory and filename
$img120_dir = "../wp-content/uploads/ShortTermImages/120px/";
$img120_file = $img120_dir . basename($_FILES["img120"]["name"]);
$img125_dir = "../wp-content/uploads/ShortTermImages/125px/";
$img125_file = $img125_dir . basename($_FILES["img125"]["name"]);
$img600_dir = "../wp-content/uploads/ShortTermImages/600px/";
$img600_file = $img600_dir . basename($_FILES["img600"]["name"]);
$imgdef_dir = "../wp-content/uploads/ShortTermImages/";
$imgdef_file = $imgdef_dir . basename($_FILES["imgdef"]["name"]);
// set file valid variable
$img120_uploadOk = 1;
$img125_uploadOk = 1;
$img600_uploadOk = 1;
$imgdef_uploadOk = 1;
// set file type
$img120_FileType = pathinfo($img120_file,PATHINFO_EXTENSION);
$img125_FileType = pathinfo($img125_file,PATHINFO_EXTENSION);
$img600_FileType = pathinfo($img600_file,PATHINFO_EXTENSION);
$imgdef_FileType = pathinfo($imgdef_file,PATHINFO_EXTENSION);
// CHECK IF IMAGE IS AN ACTUAL IMAGE FILE
// Check 120px image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img120"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img120_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img120_uploadOk = 0;
}
}
// Check 125px image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img125"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img125_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img125_uploadOk = 0;
}
}
// Check 600px image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img600"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img600_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img600_uploadOk = 0;
}
}
// Check default image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["imgdef"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img125_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img125_uploadOk = 0;
}
}
// CHECK IMAGE FILE SIZE
// Check 120px image
if ($_FILES["img120"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your 120px file is too large.</p>";
$img120_uploadOk = 0;
}
// Check 125px image
if ($_FILES["img125"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your 125px file is too large.</p>";
$img125_uploadOk = 0;
}
// Check 600px image
if ($_FILES["img600"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your 600px file is too large.</p>";
$img600_uploadOk = 0;
}
// Check default image
if ($_FILES["imgdef"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your default image file is too large.</p>";
$imgdef_uploadOk = 0;
}
// ONLY ALLOW CERTAIN FILE FORMATS
// Check 120px image
if ( $img120_FileType != "jpg" && $img120_FileType != "png"
&& $img120_FileType != "jpeg" && $img120_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$img120_uploadOk = 0;
}
// Check 125px image
if ( $img125_FileType != "jpg" && $img125_FileType != "png"
&& $img125_FileType != "jpeg" && $img125_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$img125_uploadOk = 0;
}
// Check 600px image
if ( $img600_FileType != "jpg" && $img600_FileType != "png"
&& $img600_FileType != "jpeg" && $img600_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$img600_uploadOk = 0;
}
// Check default image
if ( $imgdef_FileType != "jpg" && $imgdef_FileType != "png"
&& $imgdef_FileType != "jpeg" && $imgdef_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$imgdef_uploadOk = 0;
}
// CHECK IF UPLOAD IS VAILD AND UPLOAD
// Check 120px image
if ($img120_uploadOk == 0) {
echo "<h4>Your 120px file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["img120"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["img120"]["name"]). " has been uploaded to ..../ShorttermImages/120px/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your 120px file was not uploaded, because there was an Error.</p>";
}
}
// Check 125px image
if ($img125_uploadOk == 0) {
echo "<h4>Your file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["img125"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["img125"]["name"]). " has been uploaded to ..../ShorttermImages/125px/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your 125px file was not uploaded, because there was an Error.</p>";
}
}
// Check 600px image
if ($img600_uploadOk == 0) {
echo "<h4>Your file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["img600"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["img600"]["name"]). " has been uploaded to ..../ShorttermImages/600px/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your 600px file was not uploaded, because there was an Error.</p>";
}
}
// Check default image
if ($imgdef_uploadOk == 0) {
echo "<h4>Your file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["imgdef"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["imgdef"]["name"]). " has been uploaded to ..../ShorttermImages/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your default image file was not uploaded, because there was an Error.</p>";
}
}
?>
脚本目前无法正常运行,我不明白为什么?
orry 输出如下:
Success: File is an image - image/jpeg.
Success: File is an image - image/jpeg.
Success: File is an image - image/jpeg.
Success: File is an image - image/jpeg.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 192
Warning: move_uploaded_file(): Unable to move '/tmp/php2JSvCo' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 192 Sorry, Your 120px file was not uploaded, because there was an Error.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 208
Warning: move_uploaded_file(): Unable to move '/tmp/phplfhowF' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 208 Sorry, Your 125px file was not uploaded, because there was an Error.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 224
Warning: move_uploaded_file(): Unable to move '/tmp/phpFv6hqW' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 224 Sorry, Your 600px file was not uploaded, because there was an Error.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 240
Warning: move_uploaded_file(): Unable to move '/tmp/phpkpuhkd' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 240 Sorry, Your default image file was not uploaded, because there was an Error.
在继续之前,您是否确保填充了 $target_file 变量。查看代码 $target_file 永远不会填充任何东西。我输入了任意目录路径,我的图片已正确上传。
我正在尝试在我们的网站上创建一个上传,基本上我们可以将各种尺寸的图像添加到输入中,它们都会转到相应的目标位置。
这是表格
<form action="upload.php" method="post" enctype="multipart/form-data">
Select 120px image to upload:
<input type="file" name="img120" id="img120">
Select 125px image to upload:
<input type="file" name="img125" id="img125">
Select 600px image to upload:
<input type="file" name="img600" id="img600">
Select default image to upload:
<input type="file" name="imgdef" id="imgdef">
<input type="submit" value="Upload Images" name="submit">
</form>
这提交到一个名为upload.php的php文件,其代码如下:
// set upload target directory and filename
$img120_dir = "../wp-content/uploads/ShortTermImages/120px/";
$img120_file = $img120_dir . basename($_FILES["img120"]["name"]);
$img125_dir = "../wp-content/uploads/ShortTermImages/125px/";
$img125_file = $img125_dir . basename($_FILES["img125"]["name"]);
$img600_dir = "../wp-content/uploads/ShortTermImages/600px/";
$img600_file = $img600_dir . basename($_FILES["img600"]["name"]);
$imgdef_dir = "../wp-content/uploads/ShortTermImages/";
$imgdef_file = $imgdef_dir . basename($_FILES["imgdef"]["name"]);
// set file valid variable
$img120_uploadOk = 1;
$img125_uploadOk = 1;
$img600_uploadOk = 1;
$imgdef_uploadOk = 1;
// set file type
$img120_FileType = pathinfo($img120_file,PATHINFO_EXTENSION);
$img125_FileType = pathinfo($img125_file,PATHINFO_EXTENSION);
$img600_FileType = pathinfo($img600_file,PATHINFO_EXTENSION);
$imgdef_FileType = pathinfo($imgdef_file,PATHINFO_EXTENSION);
// CHECK IF IMAGE IS AN ACTUAL IMAGE FILE
// Check 120px image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img120"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img120_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img120_uploadOk = 0;
}
}
// Check 125px image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img125"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img125_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img125_uploadOk = 0;
}
}
// Check 600px image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["img600"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img600_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img600_uploadOk = 0;
}
}
// Check default image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["imgdef"]["tmp_name"]);
if($check !== false) {
echo "<p style='color:#458B00'><strong>Success:</strong> File is an image - " . $check["mime"] . ".</p>";
$img125_uploadOk = 1;
} else {
echo "<p style='color:#dd0000;'><strong>Error:</strong> File is not an image.";
$img125_uploadOk = 0;
}
}
// CHECK IMAGE FILE SIZE
// Check 120px image
if ($_FILES["img120"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your 120px file is too large.</p>";
$img120_uploadOk = 0;
}
// Check 125px image
if ($_FILES["img125"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your 125px file is too large.</p>";
$img125_uploadOk = 0;
}
// Check 600px image
if ($_FILES["img600"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your 600px file is too large.</p>";
$img600_uploadOk = 0;
}
// Check default image
if ($_FILES["imgdef"]["size"] > 500000) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, your default image file is too large.</p>";
$imgdef_uploadOk = 0;
}
// ONLY ALLOW CERTAIN FILE FORMATS
// Check 120px image
if ( $img120_FileType != "jpg" && $img120_FileType != "png"
&& $img120_FileType != "jpeg" && $img120_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$img120_uploadOk = 0;
}
// Check 125px image
if ( $img125_FileType != "jpg" && $img125_FileType != "png"
&& $img125_FileType != "jpeg" && $img125_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$img125_uploadOk = 0;
}
// Check 600px image
if ( $img600_FileType != "jpg" && $img600_FileType != "png"
&& $img600_FileType != "jpeg" && $img600_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$img600_uploadOk = 0;
}
// Check default image
if ( $imgdef_FileType != "jpg" && $imgdef_FileType != "png"
&& $imgdef_FileType != "jpeg" && $imgdef_FileType != "gif" ) {
echo "<p style='color:#dd0000;'><strong>Error:</strong> Sorry, only <strong>JPG</strong>, <strong>JPEG</strong>, <strong>PNG</strong> & <strong>GIF</strong> files are allowed.";
$imgdef_uploadOk = 0;
}
// CHECK IF UPLOAD IS VAILD AND UPLOAD
// Check 120px image
if ($img120_uploadOk == 0) {
echo "<h4>Your 120px file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["img120"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["img120"]["name"]). " has been uploaded to ..../ShorttermImages/120px/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your 120px file was not uploaded, because there was an Error.</p>";
}
}
// Check 125px image
if ($img125_uploadOk == 0) {
echo "<h4>Your file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["img125"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["img125"]["name"]). " has been uploaded to ..../ShorttermImages/125px/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your 125px file was not uploaded, because there was an Error.</p>";
}
}
// Check 600px image
if ($img600_uploadOk == 0) {
echo "<h4>Your file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["img600"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["img600"]["name"]). " has been uploaded to ..../ShorttermImages/600px/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your 600px file was not uploaded, because there was an Error.</p>";
}
}
// Check default image
if ($imgdef_uploadOk == 0) {
echo "<h4>Your file was not uploaded, because there was an Error.</h4>";
} else {
// if file is vaild, try uploading file
if (move_uploaded_file($_FILES["imgdef"]["tmp_name"], $target_file)) {
echo "<p style='color:#458B00;'><strong>SUCESS:</strong> The file ". basename( $_FILES["imgdef"]["name"]). " has been uploaded to ..../ShorttermImages/</p>";
} else {
echo "<p style='color:#dd0000;'> Sorry, Your default image file was not uploaded, because there was an Error.</p>";
}
}
?>
脚本目前无法正常运行,我不明白为什么?
orry 输出如下:
Success: File is an image - image/jpeg.
Success: File is an image - image/jpeg.
Success: File is an image - image/jpeg.
Success: File is an image - image/jpeg.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 192
Warning: move_uploaded_file(): Unable to move '/tmp/php2JSvCo' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 192 Sorry, Your 120px file was not uploaded, because there was an Error.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 208
Warning: move_uploaded_file(): Unable to move '/tmp/phplfhowF' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 208 Sorry, Your 125px file was not uploaded, because there was an Error.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 224
Warning: move_uploaded_file(): Unable to move '/tmp/phpFv6hqW' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 224 Sorry, Your 600px file was not uploaded, because there was an Error.
Warning: move_uploaded_file(): Filename cannot be empty in /home/cocoonvehicles/public_html/upload/upload.php on line 240
Warning: move_uploaded_file(): Unable to move '/tmp/phpkpuhkd' to '' in /home/cocoonvehicles/public_html/upload/upload.php on line 240 Sorry, Your default image file was not uploaded, because there was an Error.
在继续之前,您是否确保填充了 $target_file 变量。查看代码 $target_file 永远不会填充任何东西。我输入了任意目录路径,我的图片已正确上传。