【发布时间】:2015-12-12 08:35:28
【问题描述】:
我想知道为什么我的代码不能正确重命名我的文件。这就是我相信我的代码所做的。
- 从 POST 中获取
$_FILES(文件) - 将其移至特定文件夹 (
uploads/) - 将其重命名为日期格式(我想这样做以便没有文件具有相同的名称)
<?php
session_start();
$temps = date('Y-m-d H:i:s');
$target_dir = "uploads/";
$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) {
} else {
$_SESSION['error'] = "This is not an image -.-";
header('location:upload.php');
}
}
if ($_FILES["fileToUpload"]["size"] > 2000000) {
$_SESSION['error'] = "The uploaded image must be smaller than 2MB";
header('location:upload.php');
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$_SESSION['error'] = "Sorry, only JPG, PNG & GIF files are allowed";
header('location:upload.php');
}
if ($uploadOk == 0) {
$_SESSION['error'] = "There was an error uploading your file, please try again !";
header('location:upload.php');
}
else {
$new = ($target_dir.$temps.'.'$imageFileType);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
if (rename($target_file, $new) == true)
{
$_SESSION['error'] = "Your image has been uploadsdsaded with success ... Hurray !";
header('location:upload.php');
}
else{
$_SESSION['error'] = "There was an error uploading your file, please try again !";
header('location:upload.php');
}
} else {
$_SESSION['error'] = "There was an error uploading your file, please try again !";
header('location:upload.php');
}
}
?>
非常感谢。我是 PHP 新手,所以如果您有任何其他提示,请随时告诉他们:)
编辑:文件实际上是从临时目录移动到目标目录,只是不会被重命名。
【问题讨论】: