我正在关注本教程:http://www.w3schools.com/php/php_file_upload.asp。我已经复制并粘贴了PHP和HTML的代码。在文件夹中,我有upload.html
,php.ini
(带有file_uploads = On
),upload.php
和一个名为uploads
的文件夹,其中所有上传的文件都应该去在,我想。
当我按提交时,一切似乎都有效,即成功消息得到回应,"The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."
。但是,uploads
文件夹中似乎没有文件出现。
此外,当我尝试上传相同的文件时,收到错误消息echo "Sorry, file already exists.";
,这很奇怪,因为我在uploads
文件夹中看不到该文件。
我目前正在使用FireFTP来存储我上传的所有代码和文件。
以下是upload.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>
这是我的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 a actual image or 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 != "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.";
}
}
?>
另外,有人告诉我PHP是一种服务器端语言。这究竟意味着什么?我试着在线阅读一些东西,但它充满了这么多行话。这是否意味着我必须使用像FireFTP这样的东西才能使用像PHP这样的语言?
我无法使用chrome打开我的文本文件并能够使用PHP?就像,我右键单击包含我的代码的文本文件并执行&#34;用chrome打开#34;?这是否也与网络托管有关?
答案 0 :(得分:1)
试用此代码
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["fileToUpload"]["name"]));
if ((($_FILES["fileToUpload"]["type"] == "image/gif")
|| ($_FILES["fileToUpload"]["type"] == "image/jpeg")
|| ($_FILES["fileToUpload"]["type"] == "image/png")
|| ($_FILES["fileToUpload"]["type"] == "image/pjpeg"))
&& ($_FILES["fileToUpload"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["fileToUpload"]["error"] > 0)
{
header("Location:dashboard.php?err=imgpro1");
//echo "Return Code: " . $_FILES["fileToUpload"]["error"] . "<br>";
}
else
{
$tempupname = time().$_FILES["fileToUpload"]["name"];
$imgpathtostore="uploads/".$tempupname;
$imgpathtostoreDB="uploads/".$tempupname;
// Enter your path to upload file here
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imgpathtostore);
}
}
else
{
header("Location: index.php?err=imgpro");
}
}
答案 1 :(得分:0)
您需要设置uploads
文件夹权限读取,写入和删除。