重命名文件上载PHP
我使用的是一个使用PHP上传文件的例子,我知道它们在编码方面不是最好的,因为它可能会过时、糟糕等等,但我不需要太花哨的东西。但是我想知道上传时如何更改文件名 W3学校代码(略加编辑):重命名文件上载PHP,php,file-upload,file-rename,Php,File Upload,File Rename,我使用的是一个使用PHP上传文件的例子,我知道它们在编码方面不是最好的,因为它可能会过时、糟糕等等,但我不需要太花哨的东西。但是我想知道上传时如何更改文件名 W3学校代码(略加编辑): 我想将名称更改为仅配置文件,现在使用此代码上载时,它将以配置文件的形式上载文件,后跟文件名,如profilexxxx.jpeg。如下图所示 $target_file = $target_dir.'profile'.basename($_FILES["fileToUpload"]["
我想将名称更改为仅配置文件,现在使用此代码上载时,它将以配置文件的形式上载文件,后跟文件名,如profilexxxx.jpeg。如下图所示
$target_file = $target_dir.'profile'.basename($_FILES["fileToUpload"]["name"]);
除去
$target_file = $target_dir.'profile'.strrchr($_FILES["fileToUpload"]["name"],'.');
但你真的应该试试自己
此位strrchr($_文件[“fileToUpload”][“name”],“.”)代码>返回文件名的扩展名。因此,如果它是file.jpg
,它将返回.jpg
,从而返回profile.jpg
。我应该提到的是,它只返回最后一个分机。因此,如果您有file.php.jpg
,它只返回.jpg
更新
我没有看到您已经解析出了扩展,因此,考虑到这一点,您可以稍微移动一下
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$target_file = $target_dir.'profile'.'.'.$imageFileType;
不需要做两次
Pathinfo-可能是“正确”的方式。只是大多数沙盒都关闭了它,所以我有点懒得用谷歌搜索它,看看它是否保留了
中的jpg
vs.jpg
(我总是忘了)
PS这就是沙盒所说的
警告:出于安全原因,第7行[…][…]中的pathinfo()已被禁用
我用它上传文件,效果很好
<?php
if(isset($_POST['submit'])){
// Retrieve file from post method
$file = $_FILES['file'];
// Get file properties
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
//Separate name and file extension
$fileExt = explode('.', $fileName);
//Set to always lowercase
$fileActualExt = strtolower(end($fileExt));
//Set any extension allowed
$allowed = array('jpg','jpeg','png');
//Check whether file extension is allowed
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
//Check file size criteria
if($fileSize <= 150000){
$NewName = "MyNewName"; //define new file name
//Define your custom file name
$fileNameNew = $NewName.".".$fileActualExt;
//Define file destination
$fileDestination = '../images/'.$fileNameNew;
//php uploading files
move_uploaded_file($fileTmpName, $fileDestination);
} else{
echo "file is too big";
}
} else{
echo "upload error";
}
} else{
echo "your extension is not allowed";
}
}
?>
这是如何失败的?你能详细解释一下吗?看看这篇文章的底部,我在那里说的。我希望它以文件名profile上传,但它以profilexxxx的形式上传,例如,我上传一个名为dog.jpg的图像,它将是profiledog.jpg。我只希望它是profile.jpgit,有点不安全,攻击者无法发送$\u POST[“submit”]
并绕过您的图像验证,希望您的服务器不会解析shell.php.jpg
。@LawrenceCherone我将在稍后添加验证,现在我只需要这个就可以了。是的,上传的默认状态应该是fail$uploadOk=1
应该是$uploadOk=0代码>只有在某些条件表明它正常时才应该通过。甚至有些事情是可以修复的,例如,一旦决定上传失败,其他代码都不需要运行。因此,这只是一个打字错误。这非常有效!谢谢,很简单
<?php
if(isset($_POST['submit'])){
// Retrieve file from post method
$file = $_FILES['file'];
// Get file properties
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
//Separate name and file extension
$fileExt = explode('.', $fileName);
//Set to always lowercase
$fileActualExt = strtolower(end($fileExt));
//Set any extension allowed
$allowed = array('jpg','jpeg','png');
//Check whether file extension is allowed
if(in_array($fileActualExt, $allowed)){
if($fileError === 0){
//Check file size criteria
if($fileSize <= 150000){
$NewName = "MyNewName"; //define new file name
//Define your custom file name
$fileNameNew = $NewName.".".$fileActualExt;
//Define file destination
$fileDestination = '../images/'.$fileNameNew;
//php uploading files
move_uploaded_file($fileTmpName, $fileDestination);
} else{
echo "file is too big";
}
} else{
echo "upload error";
}
} else{
echo "your extension is not allowed";
}
}
?>