首先,我在这里问了这个问题:如何在发送到upload_file.php之前更改文件名?
我用建议更新了我的upload_file.php脚本,但是当我运行它时,它不起作用。使用上面链接中的表单,当我去上传脚本时,回显似乎是正确的,但是当我 FTP 到文件应该在的目录时,目录是空的?有人可以对此有所了解吗?我希望将文件上传到'/var/www/eedis/fax/uploads/'。
我的upload_file.php脚本看起来像这样...
<?php
$allowedExts = array("pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ($_FILES["file"]["type"] == "application/pdf")
#&& ($_FILES["file"]["size"] < 20000)
#&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/var/www/eedis/fax/uploads/" . $_POST['number'] . '.pdf');
}
}
}
else
{
echo "Invalid file";
}
?>
创建一个名为 upload.php 的 PHP 文件用于上传过程。
在您的项目目录中创建一个名为"图库"(或您想要的)的文件夹,我们希望上传的文件转到它。
$uploadDir = './gallery/';
用于返回上传过程的结果。
$result = 0;
现在处理用户输入并获取文件扩展名,然后为上传的文件创建唯一的文件名(因为不同的用户可能会上传具有相似文件名的文件)
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
//get the file extension
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
$randName = md5(rand() * time());
// creating the unique file name for the uploaded file
$filePath = $uploadDir . $randName . '.' . $ext;
if(@move_uploaded_file($tmpName, $filePath)) {
//move the uploaded file from server's temp directory to your gallery path
$result = 1;
}
sleep(1);
}
要创建用于上传文件和图像的 PHP/AJAX 完整系统,请阅读本文:
http://blueingenuity.com/articles/4/creating-a-photo-uploader-using-ajax-and-php/
问题已修复。这只是一个许可。继续下一个!
您是否在表单中使用过enctype="multipart/form-data"
?另请查看文件夹权限。