我正在使用php上传文件。我成功完成了但是当我想打印它时它给我错误plz帮助我我的PHP脚本在下面
<?php
$target_dir = "C:\wamp\www\upload\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) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} 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.";
}
if(copy($_FILES['fileToUpload']['name'], $target_dir))
{
echo "Successful<BR/>";
echo "File Name :".$_FILES['fileToUpload']['name']."<BR/>";
echo "File Size :".$_FILES['fileToUpload']['size']."<BR/>";
echo "File Type :".$_FILES['fileToUpload']['type']."<BR/>";
echo "<img src=\"$target_dir\" width=\"150\" height=\"150\">";
}
}
?>
发生以下错误
上传后应该像这样打印 enter link description here
答案 0 :(得分:1)
像这样尝试
if (is_file($target_file))
{
echo "Successful<BR/>";
echo "File Name :".$_FILES['fileToUpload']['name']."<BR/>";
echo "File Size :".$_FILES['fileToUpload']['size']."<BR/>";
echo "File Type :".$_FILES['fileToUpload']['type']."<BR/>";
echo "<img src=\"$target_file\" width=\"150\" height=\"150\">";
}
答案 1 :(得分:0)
那是因为你正在复制一个不再存在的文件..
//$_FILES['fileToUpload']['name'] doesn't exist anymore because YOU MOVED IT
copy($_FILES['fileToUpload']['name'], $target_dir)
如果你想要复制..而不是使用$ _FILE,你可以像..
那样copy($target_file, $target_dir)
变量'target_file'是您将文件移动到哪里的?
这应该有效。
答案 2 :(得分:0)
只需替换此
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.";
}
if(copy($_FILES['fileToUpload']['name'], $target_dir))
{
echo "Successful<BR/>";
echo "File Name :".$_FILES['fileToUpload']['name']."<BR/>";
echo "File Size :".$_FILES['fileToUpload']['size']."<BR/>";
echo "File Type :".$_FILES['fileToUpload']['type']."<BR/>";
echo "<img src=\"$target_dir\" width=\"150\" height=\"150\">";
}
到
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
echo "Successful<BR/>";
echo "File Name :".$_FILES['fileToUpload']['name']."<BR/>";
echo "File Size :".$_FILES['fileToUpload']['size']."<BR/>";
echo "File Type :".$_FILES['fileToUpload']['type']."<BR/>";
echo "<img src=\"$target_dir\" width=\"150\" height=\"150\">";
} else {
echo "Sorry, there was an error uploading your file.";
}
不需要if(copy($_FILES['fileToUpload']['name'], $target_dir))
,因为$_FILES["fileToUpload"]['name']
不是文件,但文件是$_FILES["fileToUpload"]["tmp_name"]
,您已经完成了