在上载PHP Mysql期间重命名图像
使用以下代码,有人能向我解释一下在上传过程中如何将图像文件重命名为杂项名称吗 这就是我的工作 uploader.php在上载PHP Mysql期间重命名图像,php,mysql,file-rename,Php,Mysql,File Rename,使用以下代码,有人能向我解释一下在上传过程中如何将图像文件重命名为杂项名称吗 这就是我的工作 uploader.php <?php include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php"); $dataType = mysql_real_escape_string($_POST["dataType"]); $title = mysql_real_escape_string($_POST["title"]); $fil
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileName = basename($_FILES["image"]["name"]);
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/".$fileName);
if (file_exists($target_path))
{
echo "An image with that file name already exists.";
}
elseif (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// The file is in the images/gallery folder. Insert record into database by
// executing the following query:
$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
您正在执行移动上传的文件($\u文件[“图像”][“tmp\u名称”],$target\u路径)
Youre$target\u路径
来自变量$fileName
将文件名从
$fileName = basename($_FILES["image"]["name"]);
到
我会尝试这样做,您将创建一个唯一的id,并将文件的扩展名附加到它,如果该名称存在,您将循环直到有一个不存在,然后移动该文件
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/connections/dbconnect.php");
$dataType = mysql_real_escape_string($_POST["dataType"]);
$title = mysql_real_escape_string($_POST["title"]);
$fileData = pathinfo(basename($_FILES["image"]["name"]));
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
while(file_exists($target_path))
{
$fileName = uniqid() . '.' . $fileData['extension'];
$target_path = ($_SERVER['DOCUMENT_ROOT'] . "/images/gallery/" . $fileName);
}
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_path))
{
// The file is in the images/gallery folder. Insert record into database by
// executing the following query:
$sql="INSERT INTO images (data_type, title, file_name)"."VALUES('$dataType','$title','$fileName')";
$retval = mysql_query($sql);
echo "The image was successfully uploaded and added to the gallery :) <a href='index.php'>Add another image</a>";
}
else
{
echo "There was an error uploading the file, please try again!";
}
请尝试以下代码:
$sql =mysql_query("select * from table ");
$result = mysql_num_rows($sql) +1;
$newfilename = rename("$oldfilename", "$newfilename");
我建议上传一个安全的图像格式并限制文件大小
//image upload
$pro_image = $_FILES['image']['name'];
$pro_image_tmp = $_FILES['image']['tmp_name'];
$fileType = $_FILES["image"]["type"];
$fileSize = $_FILES["image"]["size"];
$fileErrorMsg = $_FILES["image"]["error"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $pro_image);
$nwtourimg = explode(".", $fileName);
if (!$pro_image_tmp) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$fileExt = end($nwtourimg);
$fileName = time().rand().".".$fileExt;
move_uploaded_file("$pro_image_tmp","upload/$fileName");
//image upload end
//图像上传
$pro_image=$_文件['image']['name'];
$pro_image_tmp=$_文件['image']['tmp_名称']
$fileType = $_FILES["image"]["type"];
$fileSize = $_FILES["image"]["size"];
$fileErrorMsg = $_FILES["image"]["error"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $pro_image);
$nwtourimg = explode(".", $fileName);
if (!$pro_image_tmp) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$fileExt = end($nwtourimg);
$fileName = time().rand().".".$fileExt;
move_uploaded_file("$pro_image_tmp","upload/$fileName");
//image upload end
所以更改$fileName=basename($_FILES[“image”][“name”])
到这个$myfilename=basename($_FILES[“image”][“name”])代码>g@daugaard47您想将您的文件命名为什么?说你想叫它myfile.jpg
?所以$filename='myfile.jpg'代码>我希望它应用随机名称。1.jpg 2.jpg无关紧要。只需要应用一个随机名称,因为我的客户通过表单从他的iPhone向我发送图像,但每个图像都称为“图像”。这就是我在上传过程中需要重命名文件的原因。最后,我希望能够使用我的手机将这些图片从我的管理页面上传到多媒体资料中。@daugaard47只需使用file\u exists()
确保该文件不存在,然后按照您的建议重命名它!太好了,非常感谢!!我想知道你是否有时间,你能帮我做一些关于这个代码的其他事情吗?如果有,请发电子邮件给我chris@pdslo.com或者我们可以在这里聊天吗?
//image upload
$pro_image = $_FILES['image']['name'];
$pro_image_tmp = $_FILES['image']['tmp_name'];
$fileType = $_FILES["image"]["type"];
$fileSize = $_FILES["image"]["size"];
$fileErrorMsg = $_FILES["image"]["error"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $pro_image);
$nwtourimg = explode(".", $fileName);
if (!$pro_image_tmp) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$fileExt = end($nwtourimg);
$fileName = time().rand().".".$fileExt;
move_uploaded_file("$pro_image_tmp","upload/$fileName");
//image upload end
$fileType = $_FILES["image"]["type"];
$fileSize = $_FILES["image"]["size"];
$fileErrorMsg = $_FILES["image"]["error"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $pro_image);
$nwtourimg = explode(".", $fileName);
if (!$pro_image_tmp) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) { // if file size is larger than 5 Megabytes
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if (!preg_match("/.(gif|jpg|png|jpeg)$/i", $fileName) ) {
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($pro_image_tmp); // Remove the uploaded file from the PHP temp folder
exit();
} else if ($fileErrorMsg == 1) { // if file upload error key is equal to 1
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$fileExt = end($nwtourimg);
$fileName = time().rand().".".$fileExt;
move_uploaded_file("$pro_image_tmp","upload/$fileName");
//image upload end