I want to prepare 5 photo upload form and user need to select all 5 photo at once and the submit the form
then i need to store all 5 photos in the Target Directory by renaming image as "1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg". I stored only one image but after add For loop for save more than one file its not working ....please support.
// Image Upload FORM
<form action="saveinntion.php" method="post" enctype="multipart/form-data">
<h1>Upload Your Innovation</h1
<fieldset>
<legend><span class="number">4</span>Upload Images</legend>
<input type="file" name="img1" id="img1" >
</br>
<input type="file" name="img2" id="img2">
</br>
<input type="file" name="img3" id="img3">
</br>
<input type="file" name="img4" id="img4">
</br>
<input type="file" name="img5" id="img5">
</br>
</fieldset>
<button type="submit">Submit</button>
</form>
saveinntion.php file
<?php
include("dbconnection.php");
$target_dir = "Upload/";
$img=$_POST['img'];
for ($i = 0; $i < 5; $i++) {
$target_file = $target_dir . basename($_FILES['$img[]']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES['$img[]']["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
echo "Sorry, only JPG & JPEG files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES['$img[]']["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['$img[]']["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
header("Location: upload1.php?id=$msg");
?>
Your problem is with your naming. as #Twinfriends said in the command it is highly unlikely to upload any file
replace $_FILES['$img[]'] with $_FILES['img'.($i+1)] in your code.
line # 5
$target_file = $target_dir . basename($_FILES['img'.($i+1)]["name"]);
line #13
if ($_FILES['img'.($i+1)]["size"] > 500000) {
line #25
if (move_uploaded_file($_FILES['img'.($i+1)]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES['img'.($i+1)]["name"]). " has been uploaded.";
To renaming file.
$imageFileType = pathinfo($_FILES['img'.($i+1)]["name"],PATHINFO_EXTENSION);
$target_file = $target_dir . ($i+1).".".$imageFileType;
Related
Using www.w3schools.com/php/php_file_upload.asp as a base, I want to upload a file but change the file name to include a field in my form, plus specific test.
How can I do this? (I very new to this)
EG. file name being uploaded = TestFile.jpeg
I want the file renamed to be [first_name field in form]_invoice.jpeg
This is my form:
<form action="upload.php" method="post" enctype="multipart/form-data">
First name
<input type="text" name="first_name" id="first_name" />
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
This is my PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "pdf" && $imageFileType != "txt" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only PDF, TXT, JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Change the first lines in the the code like this:
<?php
$target_dir = "uploads/";
$imageFileType = strtolower(pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION));
$target_file = $target_dir . trim($_POST['your_name']) . '_invoice.' . $imageFileType;
$uploadOk = 1;
But you should also have to have <input type="text" name="your_name"> in your form because according your code and the article, there's no text-typed input.
I made an upload form called upload.php with action up.php so I upload images in the folder C:/xampp/htdocs/uploadimages and in the page upload.html I would like to make a dynamic slideshow containing all new and old pictures.
upload.php
<body>
<form action="up.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
<p><strong>Note:</strong> Only <b> .jpg </b> and <b> .png </b> formats allowed to a max size of 3 MB.</p>
</form>
<div class="gallery">
<div class="gallery-image">
<?php
$directory = "C:/xampp/htdocs/uploadimages";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo " <img src=\"basename($image)\"> ";
echo "<br>" ;
}
?>
</div>
</body>
up.php
<?php
$target_dir = "C:/xampp/htdocs/uploadimages/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (file_exists($target_file)) {
echo "File already exists. <br>";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 3 * 1024 * 1024) {
echo "Your file is too large. <br>";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png") {
echo "Only JPG and PNG files are allowed. <br>";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded. <br>";
} else {
echo "There was an error uploading your file. <br>";
}
}
?>
I tried the basename($image) inside a for-each loop but nothing worked
This question already has an answer here:
How i can change the image name before uploading it to folder?
(1 answer)
Closed 3 years ago.
I just need to change the image name before uploading it to folder.
Whatever user type in here:
<input name="yname" placeholder="Your Name"class="input-group-field" >
the image name will be changed to given name.
html
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Select image to upload:<br></br></h3>
<input name="yname" placeholder="Your Name"class="input-group-field" ><br>
<input type="file" class="button" id="fileToUpload" name="fileToUpload">
<input type="submit" class="button" value="Upload Images" name="submit">
</form>
<button class ="button" onclick="window.location.href = 'showimgs.php';">show images</button>
php
<?php
$target_dir = "photos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Echo "<b><button class=button onclick=location.href='images.html'>Click here to go back</button></b>";
?>
I am beginner and this is not my own php code.
Try this something like this (It will also make sure the file name is unique)
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = round(microtime(true)) . '_' . $_POST['yname']. '.' . end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename);
You can then also use the $newfilename value to push the details to your Database (If you have one)
Source: How to rename uploaded file before saving it into a directory?
I now have a code to upload a single file to my website, I want to change this to a multiple file upload. I want people to be able to select multiple files at once. Does someone know what I need to change about my code?
HTML
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
PHP
<?php
$target_dir = "gallery-images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "JPG" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
All you need to make work it on front end is simply add MULTIPLE attribute to your input - it also works with drag and drop with couple browsers.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload" multiple>
<input type="submit" value="Upload Image" name="submit">
</form>
Let me know if You want to send it one by one in ajax
Whenever I am uploading any file it says,
File is an image - image/png.Sorry, there was an error uploading your file.
I am using post method and enctype="multipart/form-data". My image file is also proper.
<?php
if(isset($_POST['submit'])){
$target_dir = "pics/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Select image to upload:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
</div>
<div class="form-group">
<label for="usr">URL:</label>
<input type="text" class="form-control" id="usr" name="url">
</div>
</div>
</div>
<button type="submit" name="submit" class="btn btn-success">Add</button>
<button type="reset" class="btn btn-warning">Reset</button>
</form>
Your file cannot be moved.
Check the path to move to
Check the permissions of the directory
Check your access to the tmp directory
You use a relative path: $target_dir = "pics/";
Change your path to:
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/pics/';