Multiple File Uploading (File Restriction error) - php

I want to upload multiple files in 1 go. I have successfully tested the code on a single file upload (removed the for each loop) then its all working. File is uploading and checking file size/file type. But when I tried to put in a for loop to be able to do multiple files, its return "This file extension is not allowed, please upload a JPEG or PNG file"
HTML Code:
<form action="fileUpload.php" method="POST" enctype="multipart/form-data">
<table>
<tr><td>File 1</td> <td><input name="file_upload[]" type="file" class="multi"/> </td></tr>
<tr><td>File 2</td> <td><input name="file_upload[]" type="file" class="multi"/> </td></tr>
<tr><td></td> <td> <input type="submit" name="upload" value="Upload"><input type="reset"> </td> </tr>
</table>
</form>
PHP code:
foreach ($_FILES['file_upload']['tmp_name'] as $key => $tmp_name)
{
$rootDir = getcwd(); // get current working directory
$uploadDirectory = "/uploads/";
$fileExtensions = ['jpeg','jpg','png', 'pdf', 'docx',]; // Get all the file extensions
$fileType = $_FILES['file_upload']['type'][$key];
// $fileExtension = strtolower(end(explode('.',$fileName)));
$fileExtension = pathinfo($_FILES["file_upload"]["tmp_name"][$key]);
$uploadPath = $rootDir . $uploadDirectory . basename($fileName);
// echo $uploadPath;
}
?>

Change :
$fileExtension = pathinfo($_FILES["file_upload"]["tmp_name"][$key]);
to :
$fileExtension = pathinfo($_FILES["file_upload"]["name"][$key]);
$fileExtension = $fileExtension['extension'];
Previously your code checks for the temporary files extension.

Related

How to Upload Multiple Images in php using function

HTML form to select multiple images.
Upload images to the server using PHP.
HTML:
<div style="margin-left:15%; margin-top:10%">
<form action="image-post-process.php" method="post" enctype="multipart/form-data" />
<table width="500px" align="center" bgcolor="blueskay">
<tr align="center">
<td colspan="2"><h2>Update Your Account</h2></td>
</tr>
<tr>
<td align="right">Customer Image:</td>
<td><input type="file" name="image[]" multiple /></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="update" value="Update Account"/></td>
<td></td>
</tr>
</table>
</form>
</div>
File upload in PHP is the most used functionality for the web application. A single file or multiple files can be easily uploaded using PHP. PHP provides a quick and simple way to implement server-side file upload functionality. Generally, in the web application, the file is uploaded to the server and the file name is stored in the database. Later the files are retrieved from the server based on the file name stored in the database.
PHP:
#image-post-process.php
function imageFunction($imageName, $imageSize, $imageTmp)
{
$ext = pathinfo($imageName, PATHINFO_EXTENSION);
$allwoed_extention = array('pdf', 'png', 'jpg','JPEG','PNG','GIF','jpeg','JPG','PDF','docx');
if(in_array($ext, $allwoed_extention)){
if ($imageSize < 10485760) {
$newfilename = round(microtime(true)) ."_".$imageSize."_".$imageName;
move_uploaded_file($imageTmp, "image/".$newfilename);
return $newfilename;
}else{
echo "File Size Not Match";
}
}else{
echo "Extension Not Match";
}
}
$count = count($_FILES['image']['name']);
for ($i=0; $i < $count; $i++)
{
$imageName = $_FILES['image']['name'][$i];
$imageSize = $_FILES['image']['size'][$i];
$imageTmp = $_FILES['image']['tmp_name'][$i];
imageFunction($imageName, $imageSize, $imageTmp);
}
From my experience, I think you need to make every image think it is part of a group. so after declaring your function, you can include a "foreach"
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {// more code goes in here}

i think move uploaded file is not working even though he can find if the file is existed

If i choose in the same folder of my target directory it said file exist but when i choose in different folder it said success but there`s no file uploaded in that folder here is my code. i get it in google
<?php
if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
$target_dir = "/home/cidinc1802/Pictures/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
?>
<form name="form" method="post" action="fortesting.php" enctype="multipart/form-data" >
<input type="file" name="my_file" /><br /><br />
<input type="submit" name="submit" value="Upload"/>
</form>

Create a PHP contact form with multiple image upload

I need to create an html contact form which collects some data and uploads 8 images through a PHP script
This is my HTML:
<form action="formmail.php" onsubmit="return controlloform()" id="form" name="form" method="POST" enctype="multipart/form-data">
<input name="email" id="email" placeholder="Email address" type="text" value="" maxlength="40">
<input name="tel" id="tel" type="text" placeholder="Order number" value="" maxlength="20">
<textarea name="msg" placeholder="Message" value="" maxlength="300"></textarea>
Upload a photo:
<p>
<p>
<input name="file" id="file" class="button" type="file" value="">
<p>
<p>
<td colspan="2">
<input type="submit" class="button" value="Submit" />
</td>
</tr>
</table>
</form>
I don't know how to create the upload script, what I need to do is uploading 2 blocks of 4 images, check the extension of the file (only jpg and png allowed) and the size (max 500 kb for image).
I've already found some upload scripts (not 100% suitable for what I need) but I don't know how to include the upload part into the rest of the code
If anyone could help me would be great
Thanks a lot
This code make multiple upload
$total = count($_FILES['photo_file']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['photo_file']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['photo_file']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
if you want to check file size insert this code
if ($_FILES["photo_file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
this code make validation
$allowed = array('gif','png' ,'jpg');
$filename = $_FILES['photo_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
$uploadOk = 0;
}

file uploaded successfully but I can't find the file

I'm using Azure server to open the website and linking to dropbox to store all the code files. I have an folder called 'image' but after uploading, though it shows "Uploaded!", I can't find that file.
Here's my code:
<?php
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset($name)){
if(!empty($name)){
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded!';
}else{
echo 'There was an error.';
}
}else{
echo 'Plz choose a file.';
}
}
?>
<form action="add.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">
</form>
Your uploaded file is in the image folder that is in the same folder of your page.php.
See your code:
$location = 'image/';
if(move_uploaded_file($tmp_name, $location.$name)){
The final location of your file is $location.$name. This is image/your_file_name.your_extension

Uploaded file not saving in folder

I have tried everything but still i cant figure out why the uploaded file is not saving anywhere.....
My HTML CODE:
<form enctype='multipart/form-data' action='generate_xml.php' method='POST'>
<table>
<tr>
<td>Enter Remote ID :</td> <td> <input type='text' name='remote' required /></td><br /> </tr><tr>
<td>Enter Alter_id : </td><td> <input type='text' name='alter' required/></td> <br /> </tr><tr>
<td>Enter Master ID : </td><td><input type='text' name='master' required/> </td><br /></tr><tr>
<td>Enter Vch ID : </td><td> <input type='text' name='vch' required/> </td><br /></tr><tr>
<td>Enter Date Account : </td><td><input type='text' name='date' required/> </td><br /></tr><tr>
<td>Choose a file to upload: <input name='uploadedfile' type='file' /></td><br /></tr>
<tr><td>
<input type='submit' value='submit' /></td>
</tr>
</table>
</form>
Here's my Code
if(! empty($_FILES['uploadedfile']['name']))
{
$this->config->load('je_settings',TRUE);
$tally_folder_path = $this->config->item('tally_folder_path');
$template_file_path = FCPATH;
$tally_folder_path="/home/torrez/Public/";
$file_type = $_FILES['uploadedfile']['type'];
$allowed = array('text/csv','text/comma-separated-values');
if( ! empty($_FILES['uploadedfile']['name']) && in_array($file_type, $allowed))
{
$tally_src_file = $tally_folder_path . basename( $_FILES['uploadedfile'] ['name']);
$name = basename( $_FILES['file']['name']);
move_uploaded_file($name, $tally_folder_path);
}
else
{
die("No file specified or Format not supported");
}
Please help!!!
Include src file path instead of file file path....
$tally_src_file = $tally_folder_path . basename( $_FILES['uploadedfile']['name']);
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $tally_src_file);
Change name to tmp_name and $_FILES['uploadedfile']
$name = $_FILES['uploadedfile']['tmp_name'];
you can try this this might help you
your code is:
$file_type = $_FILES['uploadedfile']['type'];
$allowed = array('text/csv','text/comma-separated-values');
there will be problem with allowed type array
you define it like
$allowed = array('csv','txt','jpg');
Remember to give 777 permission to the folder to which you uploading files.
I think then it will work.
as Its recommended to not use $_FILES['file']['type']
in place of that you can use
$ext = filetype ( string $filename );
it return extension of file.
can refer: http://php.net/manual/en/function.filetype.php
I hope it will help you!
I would recommend you to used the codeigniter uploading library.
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
-> Make sure that the path you are uploading is real and writable
-> Dump your $_FILES and see if it displays the right value and array index
Here is my sample code when uploading:
if (isset($_FILES['userfile']) && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$config['upload_path'] = $path = 'Your Path'; //MAKES SURE THIS PATH IS WRITABLE AND A REAL PATH
$config['allowed_types'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
//error in upload
var_dump($this->upload->display_errors());
}
else
{
//success upload
var_dump($this->upload->data());
}
}

Categories