我无法重命名上传的文件名
i can't rename uploaded file name
<?php
ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmpname = $_FILES['file']['tmp_name'];
$filetype = $_FILES['file']['type'];
$folder = "uploads/";
$f_folder = $folder.$f_filename;
$date = date("d/m/Y H:i");
$f_filename = rename($filename,$filename.$date);
if (isset($_POST['do']) and $_POST['do'] == 'upload') {
if (empty($filename)) {
echo "choose file please";
} else {
move_uploaded_file($tmpname,$f_folder);
echo "file is uploaded in ".$f_folder;
}
};
echo "
<form action='upload.php' method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='upload' />
</form>
";
?>
任何以 f_ 开头的变量,这意味着这是最终结果
错误:
Warning: rename(aaaaa.PNG,aaaaa.PNG28/06/2015 12:45)
[function.rename]: No such file or directory in (php file dir) on line
13
Warning: move_uploaded_file(uploads/) [function.move-uploaded-file]:
failed to open stream: Permission denied in (php file dir) on line 20
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\Windows\TEMP\php4A5B.tmp' to 'uploads/' in (php file dir) on
line 20 file is uploaded in uploads/
这是您的正确代码。您可以在移动时重命名文件。
<?php
ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');
if (isset($_POST['do']) and $_POST['do'] == 'upload') {
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];
$date = date("d-m-Y_H-i");
$temp = explode(".", $filename);
$new_filename = $temp[0] . '-' . $date . '.' . end($temp);
$f_folder = "uploads" . DIRECTORY_SEPARATOR . $new_filename;
if (empty($filename)) {
echo "choose file please";
} else {
move_uploaded_file($tmp_name, $f_folder);
echo "file is uploaded in " . $f_folder;
}
};
echo "
<form method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='upload' />
</form>";
?>
您的代码已损坏。在用户提交表单之前,您无法从 $_FILES
获取任何变量,并且 $_POST["do"]
(提交)的值始终为 "upload"。一些托管公司不允许您使用 move_uploaded_file()
,但不用担心,您可以改用 rename()
。先移动文件,然后重命名,两者都使用 rename()
。而且你不应该只是将文件大小设置为一个变量而不对它做任何事情,你也应该检查它,以避免在没有告诉用户任何事情的情况下拒绝太大的文件。
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
if($filesize > 41943040){
echo "File too large(max. 40MB)";
}
elseif(empty($filename)){
echo "choose file please";
}
else{
$tmpname = $_FILES['file']['tmp_name'];
$folder = "uploads/";
$date = date("d-m-Y H:i");
$f_filename = $filename. pathinfo($filename, PATHINFO_FILENAME) . $date . pathinfo($filename, PATHINFO_EXTENSION);
$f_folder = $folder . rename($tmpname,$f_folder);
echo "File was uploaded to " . $f_folder;
}
}
?>
<form action='upload.php' method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='Upload' />
</form>
<?php
ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmpname = $_FILES['file']['tmp_name'];
$filetype = $_FILES['file']['type'];
$folder = "uploads/";
$f_folder = $folder.$f_filename;
$date = date("d/m/Y H:i");
$f_filename = rename($filename,$filename.$date);
if (isset($_POST['do']) and $_POST['do'] == 'upload') {
if (empty($filename)) {
echo "choose file please";
} else {
move_uploaded_file($tmpname,$f_folder);
echo "file is uploaded in ".$f_folder;
}
};
echo "
<form action='upload.php' method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='upload' />
</form>
";
?>
任何以 f_ 开头的变量,这意味着这是最终结果 错误:
Warning: rename(aaaaa.PNG,aaaaa.PNG28/06/2015 12:45) [function.rename]: No such file or directory in (php file dir) on line 13
Warning: move_uploaded_file(uploads/) [function.move-uploaded-file]: failed to open stream: Permission denied in (php file dir) on line 20
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\TEMP\php4A5B.tmp' to 'uploads/' in (php file dir) on line 20 file is uploaded in uploads/
这是您的正确代码。您可以在移动时重命名文件。
<?php
ini_set('post_max_size', '40M');
ini_set('upload_max_filesize', '40M');
if (isset($_POST['do']) and $_POST['do'] == 'upload') {
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_type = $_FILES['file']['type'];
$date = date("d-m-Y_H-i");
$temp = explode(".", $filename);
$new_filename = $temp[0] . '-' . $date . '.' . end($temp);
$f_folder = "uploads" . DIRECTORY_SEPARATOR . $new_filename;
if (empty($filename)) {
echo "choose file please";
} else {
move_uploaded_file($tmp_name, $f_folder);
echo "file is uploaded in " . $f_folder;
}
};
echo "
<form method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='upload' />
</form>";
?>
您的代码已损坏。在用户提交表单之前,您无法从 $_FILES
获取任何变量,并且 $_POST["do"]
(提交)的值始终为 "upload"。一些托管公司不允许您使用 move_uploaded_file()
,但不用担心,您可以改用 rename()
。先移动文件,然后重命名,两者都使用 rename()
。而且你不应该只是将文件大小设置为一个变量而不对它做任何事情,你也应该检查它,以避免在没有告诉用户任何事情的情况下拒绝太大的文件。
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$filename = $_FILES['file']['name'];
$filesize = $_FILES['file']['size'];
if($filesize > 41943040){
echo "File too large(max. 40MB)";
}
elseif(empty($filename)){
echo "choose file please";
}
else{
$tmpname = $_FILES['file']['tmp_name'];
$folder = "uploads/";
$date = date("d-m-Y H:i");
$f_filename = $filename. pathinfo($filename, PATHINFO_FILENAME) . $date . pathinfo($filename, PATHINFO_EXTENSION);
$f_folder = $folder . rename($tmpname,$f_folder);
echo "File was uploaded to " . $f_folder;
}
}
?>
<form action='upload.php' method='post' enctype='multipart/form-data' >
File Path : <input type='file' name='file' />
<br>
<input type='submit' name='do' value='Upload' />
</form>