I am uploading images and cropping to a directory, how do I add a text field to change the file name when uploading?
I have tried to add name field to the form but are unable to get the form to post the newname when uploading.
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="art[]" type="file" multiple class="file-
loading">
<input type="text" name="newname"/>
<input type="submit" name="sub">
</form>
<?php
if(isset($_POST['sub'])){ (my guess is that "sub" needs to change to
"newname" but that does not seem to work)
if(isset($_FILES['art'])){
foreach ($_FILES["art"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["art"]["tmp_name"][$key];
$name = $_FILES["art"]["name"][$key];
$image = imagecreatefromstring(file_get_contents(($tmp_name)));
$filename = 'newimage/'.$name.'';
}
?>
NO error messages just doesn't append the file name.
You are missing a whole part of the actual upload (most basic example involves using move_uploaded_file). Currently, your script is just asking for data (binary file and newname) and parsing them, without doing anything. Once you got the data, you need to upload the data. at that step, you can edit the filename.
for example (taken from https://www.tutorialspoint.com/php/php_file_uploading, modified to include a new name field):
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_POST['newname'] || $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="text" name="newname" />
<input type="submit"/>
</form>
</body>
</html>
?>
Your name will be stored in $_POST['newname']
, so you could change line
$name = $_FILES["art"]["name"][$key];
to
$name = $_POST['newname'];
But there is problem, that you allow to send multiple files (attribute multiple
in input) but you have only one text field to write one filename.
This line you guessed about:
if(isset($_POST['sub']))
This tests, if you sended data by clicking on button with name "sub" (pressing enter also is considered as clicking default button).
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="art[]" type="file" multiple class="file-loading">
<input type="text" name="newname"/>
<input type="submit" name="sub">
</form>
<?php
if(isset($_POST['sub'])){
if(isset($_FILES['art'])){
foreach ($_FILES["art"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["art"]["tmp_name"][$key];
//$name = $_FILES["art"]["name"][$key];
$name = $_POST['newname'];
$image = imagecreatefromstring(file_get_contents(($tmp_name)));
$filename = 'newimage/'.$name.'.jpg';
$thumb_width = 300;
$thumb_height = 300;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image
horizontally
0 - ($new_height - $thumb_height) / 2, // Center the
image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
}
}
header("Location: assignmentart.php");
} ?>