[英]Renaming a $_FILES (file) in PHP
I would like to know why my code wont rename my file properly. 我想知道为什么我的代码无法正确重命名我的文件。 Here is what I believe my code does.
这是我相信我的代码所做的。
$_FILES
(file) from the POST $_FILES
(文件) uploads/
) 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');
}
}
?>
Thanks you very much. 非常感谢你。 I'm new to PHP so if you have any other tips please feel free to tell them :)
我是PHP的新手,所以如果您有其他建议,请随时告诉他们:)
Edit : The file actually moves from the temp directory to the target directory, It just won't get renamed. 编辑 :文件实际上从temp目录移动到目标目录,只是不会被重命名。
You could just replace 您可以更换
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
with 与
$temps = time();
$target_file = $target_dir . $temps . '.' . $imageFileType;
And the if/else statements at the bottom with 底部的if / else语句带有
if ($uploadOk == 0) {
$_SESSION['error'] = "There was an error uploading your file, please try again !";
header('location:upload.php');
} else {
if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$_SESSION['error'] = "There was an error uploading your file, please try again !";
header('location:upload.php');
}
}
The complete file. 完整文件。
<?php
session_start();
$target_dir = "uploads/";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$temps = time();
$target_file = $target_dir . $temps . '.' . $imageFileType;
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');
}
$allowed_extensions = array("jpg", "png", "jpeg", "gif");
if(!in_array($imageFileType, $allowed_extensions)) {
$_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 {
if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$_SESSION['error'] = "There was an error uploading your file, please try again !";
header('location:upload.php');
}
}
?>
you can use something like below:- 您可以使用以下内容:
$arrExtension = @explode('.', $_FILES["fileToUpload"]["name"]);
$fileName = 'prefix-'. time() . '.' . $arrExtension[1]);
File names cannot have the ":" character. 文件名不能包含“:”字符。 Try using something else like time() which will return the current timestamp or simply change the ":" character in your time to "-".
尝试使用其他类似time()的东西,它将返回当前时间戳,或将您时间中的“:”字符更改为“-”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.