上传的照片不会进入上传/文件夹
Uploaded photo's won't go into the uploads/ folder
我正在使用 w3Schools tutorial 创建我的 upload.php 文件并正确完成所有代码 - 尽管根据本教程我上传到网站的图像不会 'deposit' 到它设置的文件夹中。
我的upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);
$uploadOK = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if image file is an actual image or a fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOK = 1;
} else {
echo "File is not an image.";
$uploadOK = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOK = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOK = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG files are allowed.";
$uploadOK = 0;
}
?>
这是我尝试上传属于 'requirements' 的照片时出现的屏幕:
"uploads/" 文件夹与 upload.php 位于同一目录中,但正如我所说,这些文件仍然不会去任何地方。预先感谢您的帮助。
此致,
科迪
您还没有使用 move_uploaded_file() 函数。没有该功能,文件将不会移动到文件夹。
您需要将所有内容移动到 $_POST["submit"]
块内,然后在末尾使用 move_uploaded_file()
行
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOK = 1;
} else {
echo "File is not an image.";
$uploadOK = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOK = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOK = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG files are allowed.";
$uploadOK = 0;
}
if($uploadOK)
{
chmod($target_dir, 0750); // just to make sure the target folder is writable
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);)
{
echo 'File uploaded succesfully';
}
else
{
echo 'There was a problem uploading the file';
}
}
else
{
echo 'Upload cannot take place as the validation failed';
}
}
我正在使用 w3Schools tutorial 创建我的 upload.php 文件并正确完成所有代码 - 尽管根据本教程我上传到网站的图像不会 'deposit' 到它设置的文件夹中。
我的upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir .basename($_FILES["fileToUpload"]["name"]);
$uploadOK = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Check if image file is an actual image or a fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOK = 1;
} else {
echo "File is not an image.";
$uploadOK = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOK = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOK = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG files are allowed.";
$uploadOK = 0;
}
?>
这是我尝试上传属于 'requirements' 的照片时出现的屏幕:
"uploads/" 文件夹与 upload.php 位于同一目录中,但正如我所说,这些文件仍然不会去任何地方。预先感谢您的帮助。
此致, 科迪
您还没有使用 move_uploaded_file() 函数。没有该功能,文件将不会移动到文件夹。
您需要将所有内容移动到 $_POST["submit"]
块内,然后在末尾使用 move_uploaded_file()
行
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOK = 1;
} else {
echo "File is not an image.";
$uploadOK = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOK = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOK = 0;
}
// Allow certain file formats
if($imageFileType != "png") {
echo "Sorry, only PNG files are allowed.";
$uploadOK = 0;
}
if($uploadOK)
{
chmod($target_dir, 0750); // just to make sure the target folder is writable
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);)
{
echo 'File uploaded succesfully';
}
else
{
echo 'There was a problem uploading the file';
}
}
else
{
echo 'Upload cannot take place as the validation failed';
}
}