【问题标题】:Error displayed in php upload formphp上传表单中显示错误
【发布时间】:2017-05-15 16:42:03
【问题描述】:

我有一个 php 表单,理论上应该将图像文件上传到特定目录,但它没有这样做。这是 HTML:

<!DOCTYPE html>
<html>
<body>

<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>

</body>
</html>

这是 php:

<?php
$cartella_caricamento = "pagamenti/";
$file_caricato = $cartella_caricamento.basename($_FILES["fileToUpload"]
["name"]);
$uploadOk=1;
$imageFileType=pathinfo($file_caricato,PATHINFO_EXTENSION);
if(isset($_POST["submit"]))
{
$check = getimagesize($_FILES["fileToUpload"]["nome_upd"]);
if($check !== false)                                                            //Check if the file is an image
{
echo "Il file è un immagine, OK".$check["mime"].".";
$uploadOk=1;
} else {
echo "Il file non è un immagine.";                  
$uploadOk=0;
}
if ($_FILES["fileToUpload"]["size"] > 2000000) {                                    //check the image size, if it is >2MB it refuses it
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if ($uploadOk==0) {
echo "Il file non è stato caricato a causa di un errore";
} else {
if(move_uploaded_file($_FILES["file_caricato"]["nome_upd"], $file_caricato))
{
echo "Il file".basename($_FILES["fileToUpload"]["name"])."è stato caricato";
}else {
echo "C'è stato un errore nel caricamento del file.";
}
}
}

问题是,当我使用 XAMPP 运行页面时,它在第 21 行显示

$check = getimagesize($_FILES["fileToUpload"]["nome_upd"]);

有一个未定义的索引 (nome_tmp),我不知道为什么 而且我在资源管理器窗口中选择的图像没有上传到目录“pagamenti/”

【问题讨论】:

  • 你从哪里得到“nome_upd”?不应该只是: $check = getimagesize($_FILES["fileToUpload"]["name"]);
  • 是的,我刚刚发现有特定的名称可用于值

标签: php html forms file-upload


【解决方案1】:

您似乎对英语自动填充数组键使用意大利语措辞。我不知道是否有本地语言插件可以接受这个,但标准数组值can be found here

$_FILES['userfile']['name']
客户端计算机上文件的原始名称。

$_FILES['userfile']['type']
文件的 MIME 类型(如果浏览器提供此信息)。一个例子是“image/gif”。然而,这种 mime 类型在 PHP 端没有被检查,因此不要认为它的价值是理所当然的。

$_FILES['userfile']['size']
上传文件的大小(以字节为单位)。

$_FILES['userfile']['tmp_name']
服务器上存储上传文件的文件的临时文件名。

$_FILES['userfile']['error']
与此文件上传相关的错误代码。

因此,通过阅读您的代码,您应该使用的数组值是 tmp_name

/***
 * Will use the uploaded file path
 ***/ 
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

【讨论】:

  • 好的,解决了我的问题,不知道有具体的名字才能工作,下次记得,谢谢:D
猜你喜欢
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
相关资源
最近更新 更多