嘿大家我收到错误"未定义的索引:fileToUpload"两次,我似乎无法弄清楚为什么。错误发生在// ERROR标记所在的位置。 包含Html表单。任何帮助将不胜感激。
<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); //ERROR 1
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["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) { //ERROR 2
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<h1>Upload</h1>
<form action="Upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
答案 0 :(得分:2)
您需要检查表单是否已提交,然后在其中执行所有文件上载检查代码 类似......
scrapy crawl article_spider -a id=1 -a do_action=yes
希望它有所帮助!
答案 1 :(得分:1)
首先,您可以使用javascript
检查用户是否在客户端选择了一个文件$('form').on('submit',function(event){
if( document.getElementById("fileToUpload").files.length == 0 ){
alert("You must select a file");
event.preventDefault();
}
})
然后你也可以检查用户实际提交文件的服务器sidde php代码
if(!isset($_FILES["fileToUpload"]["name"])|| $_FILES["fileToUpload"]["size"] <1) exit();
答案 2 :(得分:0)
这实际上是因为$_FILES["fileToUpload"]
变量/数组尚未设置。你需要先进一步设置它。
您可以使用简单的if语句尝试它。
if(isset($_FILES["fileToUpload"]))
{
//your code
}