I have this code uploading a .png image. Where would I alter the code to change the name of the file to an existing $_POST['var_name'] that's also being used on the upload form?
<?php
$directory = uploaded_labels; // Name of directory that file is being saved to
$var_name = $_POST["var_name"]; // Variable name from form to change the name to
$allowedExts = array("png"); // Allowed extensions
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("$directory/" . $_FILES["file"] ["name"]))
{
echo $_FILES["file"] ["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$directory."/" . $_FILES["file"] ["name"]);
echo "Stored in: " . $directory."/" .$_FILES["file"] ["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
do you mean:
$destination = $directory."/" .$var_name;
if( move_uploaded_file($_FILES["file"]["tmp_name"], $destination) ) {
echo "Stored in: " . $destination;
}
else {
echo "file not uploaded";
}
move_uploaded_file(
$_FILES["file"]["tmp_name"],
$directory."/" . $_FILES["file"] ["name"]
);
The second parameter of this function is the destination of the moved file. This is where you can change your to be saved file name.
move_uploaded_file(
$_FILES["file"]["tmp_name"],
$directory."/" . "THE_WHATEVER_NAME_YOU_WANT_TO_USE" //could be $var_name = $_POST["var_name"];
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.