嘿,我有一个系统在上传文件。我有一个 script 我在网上找到它似乎运作良好。
这是PHP代码:
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000))
{
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'upload/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname))
{
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)))
{
echo "It's done! The file has been saved as: ".$newname;
}
else
{
echo "Error: A problem occurred during file upload!";
}
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
}
else
{
echo "Error: No file uploaded";
}
如果我想上传jpg文件,这不行。但我希望能够将文件放入另一个目录。因为目前上传页面是针对管理员用户的,所以他们位于名为admin.mysite.com的子域名中,但我想要文件的位置位于成员部分,即mysite.com/members/video/
现在有一些代码,我不是100%与“dirname( FILE )”这样做有什么作用?我猜它会得到当前的位置,但我改变了整条线,所以它看起来像这样:
$newname = '../mysite.com/members/video/'.$filename;
和
$newname = 'http://www.mysite.com/members/video/'.$filename;
但没有。任何人都知道如何更改此代码,以便我可以将文件复制到新目录?
感谢您的帮助。
答案 0 :(得分:0)
将$ newname更改为您想要的任何位置!
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'upload/'.$filename;
答案 1 :(得分:0)
dirname(_ FILE _)返回文件的当前目录,在本例中为file_upload.php
因此,在此脚本中,$ newname会将上传的文件保存到/upload/name_of_new_file_uploaded.ext。
您应该尝试使用realpath()而不是dirname。像这样:
$newname = realpath("../../members/video/") . $filename;
取决于您的文件结构,添加/删除点。
PS:请记住更改文件夹权限,以便php可以在文件夹上书写。
答案 2 :(得分:-1)
为了安全起见,你应该