where is the mistake in my script? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Friends, what is the problem in my php multifile uploading script? When running this script, it do nothing!! please help me to find out the mistake.
Thanks in advance..Here is the html code..
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload your files here <small>(1GB max)</small> ..</h3>
<br>
<br>
<input type="file" name="file" id="file">
<br>
<input type="file" name="file" id="file">
<br>
<br>
<input type="submit" value="Upload file(s)" name="submit" id="submit">
</form>
And here is the "upload.php " file..
<?php
if (isset($_POST['submit'])) {
//upload and rename file
$filename=$_FILES["file"]["name"];
$tmp_filename=$_FILES["file"]["tmp_name"];
$count_tmp_filename=count($tmp_filename);
for($i = 0; $i < $count_tmp_filename; $i++){
$file_basename = substr($filename[$i], 0, strripos($filename[$i], '.'));
$file_ext = substr($filename[$i], strripos($filename[$i], '.'));
$filesize = $_FILES["file"]["size"][$i];
$allowed_file_types = array('.doc','.docx','.rtf','.pdf','.jpg','.png','.mp4','.mp3');
if (in_array($file_ext,$allowed_file_types) && ($filesize < 1000000000000)) {
// Rename file
$newfilename = 'droidsolve_'.$file_basename.$file_ext;
if (file_exists("upload/" . $newfilename)) {
// file already exists
error echo "You have already uploaded this file.";
} else{
move_uploaded_file($_FILES[$tmp_filename[$i]], "upload/" . $newfilename);
echo "File uploaded successfully.";
}
}
}
}
?>

As chris85 pointed out in the comments a name is unique in a form set. So change your code either to (leave out one of the file inputs):
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload your files here <small>(1GB max)</small> ..</h3>
<br>
<br>
<input type="file" name="file" id="file">
<br>
<br>
<input type="submit" value="Upload file(s)" name="submit" id="submit">
</form>
Or to (making it an array and adjust the ids):
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload your files here <small>(1GB max)</small> ..</h3>
<br>
<br>
<input type="file" name="file[]" id="file1">
<br>
<input type="file" name="file[]" id="file2">
<br>
<br>
<input type="submit" value="Upload file(s)" name="submit" id="submit">
</form>
With the latter you will need to change your PHP upload code accordingly (e.g. with a foreach loop).

Related

Upload files with multiple inputs

I have a form where i want to upload files with multiple inputs.My form looks like:
<form action="" method="post">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
I do not know how to process this form..
You form doesn't work until you don't include 'enctype="multipart/form-data"', because it is necessary to use input type file.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
Now browse the file and submit the form. You will get all file data inside $_FILES. so to check what you get inside the file data, you can use :
echo '<pre>';
print_r($_FILES)
I'm not sure whether you have gone through the tutorials before, however below is the code which will help you to process it.
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
If you upload the file, you can get the files from,
$_FILES global array, i.e. $_FILES['tax'] and $_FILES['ta'].
More info can be found on php.net
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
<?php
//print_r($_POST);
if(isset($_POST['submit'])){
$name = $_FILES['tax']['name'];
$name1 = $_FILES['ta']['name'];
$temp_name = $_FILES['tax']['tmp_name'];
$temp_name1 = $_FILES['ta']['tmp_name'];
var_dump($_FILES);
if(isset($name)){
if(!empty($name)){
var_dump($_FILES);
$location = 'images/'.$name;
if(move_uploaded_file($temp_name, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
if(isset($name1)){
if(!empty($name1)){
var_dump($_FILES);
$location = 'images/'.$name1;
if(move_uploaded_file($temp_name1, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>

Multi image upload

I'm learning PHP and can not find a way to make this work. Did I write correct code to upload multiple images?
I can not think of a reason why this should be wrong.
$imageName = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image, drie1, drie2, drie3, add, strip"]["type"]);
HTML
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
PHP (file-upload.php)
var_dump($_FILES);
This will display the info of the uploaded files
You can add accept attribute to the input to limit the allowed filetypes by extention
Html
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image_file[]" multiple=""> /* multiple tag is used to upload multiple files */
<input type="submit" name="Submit" value="Submit" />
</form>
Php
<?php
foreach($_FILES["image_file"]["name"] as $key => $value)
{
$name = $value;
$tmp_name = $_FILES["image_file"]["tmp_name"][$key];
$type = $_FILES["image_file"]["type"][$key];
echo $name." , ".$tmp_name." , ".$type."\n";
}
?>

How to upload images to the disk drive of server?

I am making a Tuition Teacher finding website.I have made a sign up form. I would like to add a profile picture option to the form.
Here is the code for it,
<input type="file" name="fileToUpload" id="fileToUpload">
How do I store the image in the disk drive of the server computer?
use php move_uploaded_file
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
move_uploaded_file(file,newloc)
file:- Specifies the file to be moved
newloc:- Specifies the new location for the file
First of all, your form should have attribute enctype="multipart/form-data". You can also use copy(file,to_file).
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
if (strlen($_FILES['fileToUpload']['tmp_name']) > 0) {
$path="path/to/new-location/IMAGENAME-With-Extension";
copy($_FILES['fileToUpload']['tmp_name'],$path);
}

Upload multiple image files and display on the next page. PHP

I am attempting to upload multiple images at once, and then on submit display those images on the page. This is going to be used with mPDF. I am using the examples in the manual at http://mpdf1.com/manual/index.php?tid=467
It has a text box and 1 image uploader, and displays what ever was in the text box and the image on the next page. How can I convert this to use multiple images?
Page 1:
<?php
$html = '
<html>
<body>
<form action="example_userinput2.php" method="post" enctype="multipart/form-data">
Enter text:
<br />
<textarea name="text" id="text"></textarea>
<br />
<label for="file">Choose Image to upload:</label> <input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
';
echo $html;
exit;
?>
Page 2: (also more specifically what I change the areas I marked ** **, after allowing multiple images.)
<?php
if (($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg")
& $_FILES["file"]["size"] < 20000) {
// If the destination file already exists, it will be overwritten
move_uploaded_file($_FILES["file"]["tmp_name"], "../tmp/" . $_FILES["file"]["name"]);
}
else {
echo "Invalid file";
}
$html ='
<html>
<body>
<div>'.$_POST['text'].'</div>
**<img src="' ."../tmp/" . $_FILES["file"]["name"].'" />**
<form action="example_userinput3.php" method="post" enctype="multipart/form-data">
<textarea style="display:none" name="text" id="text">'.$_POST['text'].'</textarea>
**<input type="hidden" name="filename" id="filename" value="'. $_FILES["file"]**["name"].'" />
<input type="submit" name="submit" value="Create PDF file" />
</form>
</body>
</html>
';
echo $html;
exit;
?>
Page 3 goes to the mPDF generator so I can convert this to PDF for another project I have in mind.
Any help would be awesome.
From php manual, to find here: http://php.net/manual/en/features.file-upload.multiple.php
<form action="example_userinput2.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
On page2, you can continue with a loop and handle those files at once:
foreach ($_FILES['array_of_files'] as $position => $file) {
// should output array with indices name, type, tmp_name, error, size
var_dump($file);
}
You can do the same as with one file in the loop
You can have multiple <input type="file"> html elements set up on your page set up this way:
<input type="file" name="file[0]" />
<input type="file" name="file[1]" />
etc.
And then in PHP loop through them:
foreach($_FILES['file'] as $file){
//refer to everything as $file instead of $_FILES['file']
}
That should be enough to get you started.

Upload an image and store it in two different directories

Hi, I have a form which has two file uploaders. I want to have the second file uploader upload an image and store it in two different folders.
Can anyone help me out? Here is my HTML code:
<input type="file" name="file" id="file"><br>
<input type="submit" name="upload" value="Upload Image">
<input type="file" name="thumb" id="thumb"><br>
<input type="submit" name="updthumb" value="Upload Thumb impression">
Here is my PHP code:
if(isset($_POST["upload"])){
$image ="upload/";
move_uploaded_file($_FILES["file"]["tmp_name"], $image . $_FILES["file"]["name"]);
$newfilepath2 = $image . $_FILES["file"]["name"];
mysql_query("UPDATE `candidate` SET `imgpath`='$newfilepath2' WHERE vouchno='$vouch'") or die(mysql_error());
header("refresh: 1; candproc.php?vouchno=$vouch");
}
Can anyone help in telling me how to upload the image and save it in a different folder?
Something like:
$thumbspath ="thumbs/";
$thumb = $thumbspath . $_FILES["thumb"]["name"];
move_uploaded_file($_FILES["thumb"]["tmp_name"], $thumb);
For the second file, saving into the directory thumbs
You should have set your form:
<form action="phpscript.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="file" name="thumb" id="thumb"><br>
<input type="submit" name="upload" value="Upload files">
</form>
Note that enctype="multipart/form-data" is required for file uploads.

Categories