I have an 'Upload-File' field in my form for my site visitors to upload images.
<input type="file" name="fileToUpload" id="fileToUpload">
Currently, the file is saved to my server with the same 'name' it was uploaded with.
I would like to add an input field to my form (like the one below) so the user may enter a new 'name' for the [image] file.
<input name="imageRename" id="imageRename">
I need some advice on altering the current php code to rename file before it saves to my server.
Here is the current php code for image handling:
<?php
$target_dir = "articles/article-images/";
$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)
{
echo "". $check[""].""; $uploadOk = 1;
}
else
{
echo "× FILE IS NOT AN IMAGE"; $uploadOk = 0;
}
}
if(file_exists($target_file))
{
echo "× THIS IMAGE ALREADY EXIST ON SERVER"; $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo "× FILE IS TOO LARGE"; $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
echo "× ONLY JPG, JPEG, PNG & GIF FILES ARE PERMITTED"; $uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "× IMAGE WAS NOT UPLOADED";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
echo '<img class="fixed-ratio-resize" src="../../articles/article-images/'. basename( $_FILES["fileToUpload"]["name"]). '">';
}
else
{
echo "× IMAGE WAS NOT UPLOADED";
}
}
?>
A example for my comment:
file to upload call image_10.jpg
file name that you whant imag_xpto.jpg
code to change:
$target_dir = "articles/article-images/";
$target_new_name = "imag_xpto.jpg";
//your code...
$target = $target_dir.$target_new_name;
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
//Your code...
If you want a field to give the new name, you only need to create a input field in your html and use this code to get it.
code for post:
HTML:
<input type = "text" name = "inputfieldname">
PHP:
$target_new_name = $_POST['inputfieldname'];
Your code with some tips for debugging and improving Your code
<?php
if(isset($_POST["submit"]))
{
$target_new_name = $_POST["inputfieldname"]; //you can and you should protect, if you are sending null or empty or undefined it can cause problems in the future. For protect you can use isset and verify if the name it's null or "".
$target_dir = "articles/article-images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$target_new_file_name = $target_dir.$target_new_name.".".$imageFileType; // Check with an "error_log($target_new_file_name);" the string "$target_new_file_name". Because it is in the creation of the new string, that the problems occur
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false)
{
echo "". $check[""].""; $uploadOk = 1;
}
else
{
echo "× FILE IS NOT AN IMAGE"; $uploadOk = 0;
}
}
//if(file_exists($target_file)) - You should now verify that the new file name exists on the server
if(file_exists($target_new_file_name))
{
echo "× THIS IMAGE ALREADY EXIST ON SERVER"; $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000)
{
echo "× FILE IS TOO LARGE"; $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
echo "× ONLY JPG, JPEG, PNG & GIF FILES ARE PERMITTED"; $uploadOk = 0;
}
if ($uploadOk == 0)
{
echo "× IMAGE WAS NOT UPLOADED";
}
else
{
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_new_file_name))
{
echo '<img class="fixed-ratio-resize" src="'.$target_new_file_name.'">'; // I'm not really sure about this part of the code, I'm used to another type of html creation in php
}
else
{
echo "× IMAGE WAS NOT UPLOADED";
}
}
?>
I have not had a chance to test so there may be some errors in the code.
Simple way. Any questions, just ask that I will try to answer