Hi there I want to make a function for me to able upload a multiple image in one submission below are my code structure:
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Image1 :<input name="image1" type="file" /></p>
<p>Image2 :<input name="image2" type="file" /></p>
<p>Image3 :<input name="image3" type="file" /></p>
<input type="submit" value="Submit" />
</form>
include('configdb.php');
$uploadDir = 'upload/';
if(isset($_POST['submit']))
{
$fileName = $_FILES['image1']['name'];
$tmpName = $_FILES['image1']['tmp_name'];
$fileSize = $_FILES['image1']['size'];
$fileType = $_FILES['image1']['type'];
$image1path = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, "$image1path");
if (!$result) {
echo "Error uploading";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$image1 = addslashes($image1path);
}
$sql="INSERT INTO picture (image1, image2, image3) VALUES ('$image1','$image2', $image3)";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
Basically my code above does one image upload, How can I make a function to be able upload 3 images.
Here is the code to multiple upload image. You can upload more than 3 images.
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Image1 :<input name="image[]" type="file" /></p>
<p>Image2 :<input name="image[]" type="file" /></p>
<p>Image3 :<input name="image[]" type="file" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
include('configdb.php');
$uploadDir = 'upload/';
if(isset($_POST['submit'])) {
$image = array();
foreach($_FILES['image']['name'] as $index => $name) {
if($_FILES['image']['error'][$index] == 4) {
continue;
}
if($_FILES['image']['error'][$index] == 0) {
$fileName = $_FILES['image']['name'][$index];
$tmpName = $_FILES['image']['tmp_name'][$index];
$fileSize = $_FILES['image']['size'][$index];
$fileType = $_FILES['image']['type'][$index];
if(($fileType == "image/gif" ||
$fileType == "image/jpeg" ||
$fileType == "image/pjpeg" ||
$fileType == "image/png" ||
$fileType == "image/x-png") &&
$fileSize < 500000) {
$imagePath = $uploadDir . $fileName;
$result = #move_uploaded_file($tmpName, $imagePath);
if (!$result) {
echo "Error uploading";
exit;
}
$image[] = $imagePath;
}
}
}
// Save images to database
$nbImage = count($image);
if($nbImage) {
$sql = "INSERT INTO picture (image1, image2, image3) VALUES (";
for($i=0; $i<$nbImage; $i++) {
if($i) $sql .= ",";
$sql .= "\"".$image[$i]."\"";
}
$sql .= ")";
#mysql_query($sql);
}
}
?>
Note: you should test the type and the size of image before upload it because of the security.
Try something like this:
...
$images = array();
foreach (array('image1', 'image2', 'image3') as $name) {
$images[$name] = new stdClass();
$images[$name]->fileName = $_FILES[$name]['name'];
$images[$name]->tmpName = $_FILES[$name]['tmp_name'];
$images[$name]->fileSize = $_FILES[$name]['size'];
$images[$name]->fileType = $_FILES[$name]['type'];
$images[$name]->path = $uploadDir . $images[$name]->fileName;
$result = move_uploaded_file($images[$name]->tmpName, $images[$name]->path);
if (!$result) {
echo "Error uploading";
exit;
}
$images[$name]->sql = mysql_real_escape_string($images[$name]->path);
}
$sql="INSERT INTO picture (image1, image2, image3) VALUES ({$images['image1']},{$images['image2']},{$images['image3']})";
...
Related
I'm trying to create a form to upload a file, the problem is that the file won't be uploaded. in my code it returns "Image not uploaded".
I've searched a lot online and all the examples uses the same code.
Code:
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("/images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = '/images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype=”multipart/form-data”>
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
The form has also other fields and the data are correctly send to the server.
Try this
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = 'images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype="multipart/form-data">
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
I can't upload a file in my server using php. The problem is that I can't find see which is the error, or I don't know how see it. By the way, I think is something about the file moving. This is the php code
<!-- upload -->
<?php
if (isset($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
// File prop
$myFileName = $myFile["name"];
$myFileTmp = $myFile["tmp_name"];
$myFileSize = $myFile["size"];
$myFileError = $myFile["error"];
//File extension
$myFileExt = explode(".", $myFileName);
$myFileExt = strtolower(end($myFileExt));
$allowed = array ('png' , 'jpg' , 'txt');
if(in_array($myFileExt, $allowed)) {
if($myFileError === 0) {
$newFileName = uniqid('', true) . '.' .$myFileExt;
$fileDestination = "/var/www/upload".$newFileName;
if(move_uploaded_file($myFileTmp, $fileDestination)) {
print_r($fileDestination);
} else {
print_r($myFileError);
}
} else {
print_r("error");
}
} else {
print_r("error");
}
}
?>
Here is the form:
<form action="" method="post" enctype="multipart/form-data" style="margin:15px">
<input type="file" style="margin:5px" name="myFile">
<input type="submit" class="btn-upload-file" style="margin:5px" value="Upload">
</form>
Any idea?
Your issue is very Minor...
You just missed a Slash(/) after the /www/uploads.
Try this:
<?php
if (isset($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
// File prop
$myFileName = $myFile["name"];
$myFileTmp = $myFile["tmp_name"];
$myFileSize = $myFile["size"];
$myFileError = $myFile["error"];
//File extension
$myFileExt = explode(".", $myFileName);
$myFileExt = strtolower(end($myFileExt));
$allowed = array ('png' , 'jpg' , 'txt');
if(in_array($myFileExt, $allowed)) {
if($myFileError === 0) {
$newFileName = uniqid('', true) . '.' . $myFileExt;
$fileDestination = "/var/www/upload/{$newFileName}"; //YOU WERE ONLY MISSING A SLASH (/) HERE AFTER /upload
if(move_uploaded_file($myFileTmp, $fileDestination)) {
print_r($fileDestination);
} else {
print_r($myFileError);
}
} else {
print_r("error");
}
} else {
print_r("error");
}
}
?>
<form action="" method="post" enctype="multipart/form-data" style="margin:15px">
<input type="file" style="margin:5px" name="myFile">
<input type="submit" class="btn-upload-file" style="margin:5px" value="Upload">
</form>
I am trying to upload multiple photos at the same time but there seems to be an error with my script. If for example, I select 10 different photos, one particular image is uploaded 10 times (ignoring the other 9 images). This is the script:
for ($i = 0; $i < count($_FILES["_photo"]["name"]); $i++) {
if (!empty($_FILES['_photo']['name'][$i])) {
if (($_FILES['_photo']['type'][$i] == 'image/jpeg') OR ($_FILES['_photo']['type'][$i] == 'image/png') OR ($_FILES['_photo']['type'][$i] =='image/gif')) {
$upload_folder = "./profile_pix/";
$pic_name = time() . ".jpg";
$pic_path = $upload_folder . $pic_name;
require_once "include/resize.php";
if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path)) {
$image = new Resize($pic_path);
$image->resizeImage(180, 180, 'crop');
$image->saveImage($pic_path);
}
$sql2 = "INSERT INTO photos
(photo, member_id, photo_id)
VALUES
('$pic_name', :session_id, :offer_count)";
$db -> query($sql2, array('session_id' => $_SESSION['id'], 'offer_count' => $offer_count));
}else {
header ("Location: submitoffer.php?err=03");
}
}
HTML:
<input type="file" id="_photo" name="_photo[]" multiple="multiple">
File upload is working fine.
The line
$pic_name = time() . ".jpg";
is always evaluating to same value.
As logically all files are getting uploaded on same time().
Instead use:
$pic_name = $i . '_'. time() . ".jpg";
To add uniqueness.
try this, and you will see what is happening:
<?php
echo "<pre>";
print_r($_FILES);
echo "</pre>";
?>
And in your HTML, make sure you use different names, like this:
<input name="userfile[]" type="file" />
<input name="userfile[]" type="file" />
<input name="userfile[]" type="file" />
Then pick them up by each name. Just inspect the content of $_FILES to understand the structure.
I also advice you serious do some error checking.
100% working and tested code:
upload.php:
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_FILES['files'])){
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if($file_type == "image/jpg" || $file_type == "image/png" || $file_type == "image/jpeg" || $file_type == "image/gif" ) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
}
}
header('location:uploads.html');
}
}
else { header('location:404.html'); }
?>
upload.html
<form id="upload_images" action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]" id="file" multiple="true" />
<input type="submit" id="btn_upload" value="U P L O A D" />
</form>
Below is a php function of my post method. I am not receiving any errors but however it is not uploading into the folder I have assigned to.
I am using a localhost and there are no errors regarding the database!
<div id="box">
<form method='post' enctype="multipart/form-data">
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['name'];
$random_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'mp4' && $type != 'flv'){
$message = "Video Format Not Supported!";
}
else {
move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type);
mysql_query("INSERT INTO videos VALUES('.' '$name'; 'videos/$random_name.$type')");
$message = "Successfully Uploaded!";
}
echo "$message";
}
?>
Select Video : <br/>
<input type='file' name="video" />
<br/><br/>
<input type="submit" value="Upload" />
</form>
i have a problem in multiple image upload, when i upload, there's only 1 image which go to the targeted directory and database. i need two images to go directory and database...any help please?
this is my form
<form action="test.php" method="post" enctype="multipart/form-data">
Keywords: <input style="margin-left:35px;" type="text" name="keyword" /><br />
Name: <input style="margin-left:60px;" type="text" name="name" /><br />
Name2: <input style="margin-left:60px;" type="text" name="name2" /><br />
Categorie: <input style="margin-left:40px;" type="text" name="categorie" /><br />
Lieu: <input style="margin-left:70px;" type="text" name="lieu" /><br /><br />
<input type="file" name="file[]" multiple="multiple" /><input type="file" name="file_2[]" multiple="multiple"/><input type="submit" name="submit" value="Upload" />
</form>
<?php
$connect = mysql_connect("localhost", "root", "");
$select_db = mysql_select_db("yakatrouver_test", $connect);
if(#$_POST['submit']){
$keywords = $_POST['keyword'];
$name = $_POST['name'];
$name2 = $_POST['name2'];
$categorie = $_POST['categorie'];
$lieu = $_POST['lieu'];
$file = $_FILES['file'];
$file_name = $file['name'];
$file_type = $file['type'];
$file_size = $file['size'];
$file_path = $file['tmp_name'];
$file_2 = $FILES['file_2'];
$file_name_2 = $file['name'];
$file_type_2 = $file['type'];
$file_size_2 = $file['size'];
$file_path_2 = $file['tmp_name'];
if($file_name!="" && ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'pictures_uploaded/' .$file_name))
$query = mysql_query("INSERT INTO `user_input`(keyword, name, categorie, lieu) VALUES ('$keywords', '$name', '$categorie', ' $lieu')");
$query = mysql_query("UPDATE `user_input` set image='pictures_uploaded/$file_name' WHERE `name`='$name'");
if($query == true)
{
echo "file Uploaded";
}
}
$result = mysql_query("SELECT * FROM `user_input` WHERE `image`=''") or die(mysql_error());
while($row = mysql_fetch_array($result))
?>
Try:-
HTML:
<input type="file" name="file[]"/>
<input type="file" name="file[]"/>
<input type="file" name="file[]"/>
PHP:
$file_count = count($_FILES['file']['name']);
for($i=0;$i<$file_count;$i++) {
$file_name = $_FILES['file']['name'][$i];
$file_type = $_FILES['file']['type'][$i];
$file_size = $_FILES['file']['size'][$i];
$file_path = $_FILES['file']['path'][$i];
if($file_name!="" && ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif") && $file_size<=2000000)
if(move_uploaded_file ($file_path, 'pictures_uploaded/' .$file_name))
$query = mysql_query("INSERT INTO `user_input`(keyword, name, categorie, lieu) VALUES ('$keywords', '$name', '$categorie', ' $lieu')");
$query = mysql_query("UPDATE `user_input` set image='pictures_uploaded/$file_name' WHERE `name`='$name'");
if($query == true)
{
echo "file Uploaded";
}
}
} //close the braces accordingly, some seem to be missing in your code
I think it's self-explanatory. It's just looping through your file input array, fetching each file information and processing one at at time.