File not uploading into the map - php

This is my html code:
<form action="producttoevoegen.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="Upload"></label>
<input type="file" name="Upload[]" multiple="multiple" id="Upload" />
<input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
</form>
This is my php code:
<?php
if(isset($_FILES['Upload'])){
$UploadName = $_FILES['Upload']['name'];
$UploadType = $_FILES['Upload']['type'];
$FileSize = $_FILES['Upload']['size'];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 125000)){
die("Error - File too Big");
}
for($i=0; $i<count($UploadName); $i++) {
$tmpFilePath = $_FILES['Upload']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = /upload/" . $UploadName[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
}
When I try to upload files it doesn't work. It doesn't show in the map. I've tried a lot of things but nothing worked. Does anyone see the mistake I've made? Thanks in advance!!

if(isset($_FILES['Upload'])){
for($i=0; $i<count($UploadName); $i++) {
$UploadName = $_FILES['Upload']['name'][$i];
$UploadType = $_FILES['Upload']['type'][$i];
$FileSize = $_FILES['Upload']['size'][$i];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 125000)){
die("Error - File too Big");
}
$tmpFilePath = $_FILES['Upload']['tmp_name'][$i];
if ($tmpFilePath != "") {
$newFilePath = "/upload/" . $UploadName[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
}

Please find updated code
<form action="producttoevoegen.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="Upload"></label>
<input type="file" name="Upload" multiple="multiple" id="Upload" />
<input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
</form>
Use this code for PHP script which is given by #Minesh Patel
if(isset($_FILES['Upload'])){
for($i=0; $i<count($UploadName); $i++) {
$UploadName = $_FILES['Upload']['name'][$i];
$UploadType = $_FILES['Upload']['type'][$i];
$FileSize = $_FILES['Upload']['size'][$i];
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
if(($FileSize > 125000)){
die("Error - File too Big");
}
$tmpFilePath = $_FILES['Upload']['tmp_name'][$i];
if ($tmpFilePath != "") {
$newFilePath = "/upload/" . $UploadName[$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
}

Related

How to upload multiple image with array?

I have a form, How to insert in database one by one.For example :-
<form method="post">
<input type="file" name="img[]">
<input type="file" name="img[]">
<input type="file" name="img[]">
</form>
if(isset($_POST['submit'])){
if(count($_FILES['img']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = "uploaded/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
this may help you

Having issue in multiple image upload? db image attached

<form name="property" action="" method="post" enctype="multipart/form-data">
Select Image
<input type="file" name="upload[]" multiple="multiple" id="image"/>
<input type="submit" value="Submit" name="submit" style="width:200px; height:30px;" />
</form>
MY Php code:
if(isset($_POST['submit']))
{
$total = count($_FILES['upload']['name']);
for($i=0; $i<$total; $i++)
{
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$newFilePath[] = "upload/" . $_FILES['upload']['name'][$i];
$values_insert = implode(',', $newFilePath);
if(move_uploaded_file($tmpFilePath, $newFilePath))
{
$date=date("Y-m-d");
}
}
}
$d=mysql_query("INSERT INTO properties (iname,cdate) VALUES('".$values_insert."',NOW())") or die(mysql_error());
}
Note:
In database the path upload directory folder and image image is going to database, I want only image name separated by comma. for example the expected output is Chrysanthemum.jpg,Desert.jpg. I don't know what is the problem. please guide me.kindly look at my code and image attachment details.
Thing this'll do fyi - indenting code would help people read and help you.
if(isset($_POST['submit']))
{
$total = count($_FILES['upload']['name']);
for($i=0; $i<$total; $i++)
{
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
if ($tmpFilePath != "")
{
$newFilePath = "upload/" . $_FILES['upload']['name'][$i];
$newFilePath2[] = $_FILES['upload']['name'][$i];
$values_insert = implode(',', $newFilePath2);
if(move_uploaded_file($tmpFilePath, $newFilePath))
{
$date=date("Y-m-d");
}
}
}
$d=mysql_query("INSERT INTO properties (iname,cdate) VALUES('".$values_insert."',NOW())") or die(mysql_error());
}

File upload with php issue moving file

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>

post multiple image using 1 field in a form

My code in here. how can I upload more then 1 image using 1 input field?
<?php
if (isset($_POST['beopen_form'])) {
if ($_FILES["file1"]["error"] > 0) {
echo "Error: " . $_FILES["file1"]["error"] . "<br>";
} else {
$x = mysql_insert_id();
$path_array = wp_upload_dir();
$old_name = $_FILES["file1"]["name"];
$split_name = explode('.', $old_name);
$time = time();
$file_name = $time . "." . $split_name[1];
$tmp_name = $_FILES["file1"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $path . "/" . $old_name);
$path2 = $path . "/" . $old_name;
mysql_query("INSERT INTO carimage (image,carid,created,updated) VALUES ('$path2','$x','$created','$updated') ON DUPLICATE KEY UPDATE image='$path2',description='$makeid',updated='$updated'");
}
}
?>
<form enctype="multipart/form-data" class="beopen-contact-form" id="signupForm" novalidate="novalidate" action="<?php echo get_permalink(); ?>" method="post">
<input type="file" name="file" id="file">
<div id="recaptcha_div"></div><br>
<input type="hidden" name="beopen_form" value="1" /><!-- button -->
<button class="button send-message" type="submit">
<span class="send-message"></span><?php _e('Update', 'beopen');?>
</button>
</form>
html :-
<input type="file" name="file" id="file" multiple>
PHP CODE:-
//Loop through each file
for($i=0; $i<count($_FILES['file']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['file']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['file']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
I TAKE THIS CODE FROM :-
Multiple file upload in php
Before Post any question you should search for problem....!!!

multiple upload image function php?

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']})";
...

Categories