我无法弄清楚如何在我拥有的文件上传脚本中重命名文件名。
任何帮助都非常感谢!!
以下是我正在处理的脚本。
<?php
$target_dir = "../user_profile_pictures/";
$target_file = $target_dir . basename($_FILES["profile_pic"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Lo siento, el archivo ya existe.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png") {
echo "Lo siento, solo se permiten imagenes tipo jpg, jpeg o png.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Lo siento, el imagen no se ha subido.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $target_file)) {
echo "El imagen ". basename( $_FILES["profile_pic"]["name"]). " se ha subido.";
} else {
echo "Ha habido un error al subir el archivo.";
}
}
?>
尝试此操作,我已在您的原始代码中添加了一个newname变量。
<?php
$target_dir = "../user_profile_pictures/";
$target_file = $target_dir . basename($_FILES["profile_pic"]["name"]);
$newname = "chosen-name";
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Lo siento, el archivo ya existe.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png") {
echo "Lo siento, solo se permiten imagenes tipo jpg, jpeg o png.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Lo siento, el imagen no se ha subido.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $target_dir.$newname)) {
echo "El imagen ". basename( $_FILES["profile_pic"]["name"]). " se ha subido.";
} else {
echo "Ha habido un error al subir el archivo.";
}
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句