It doesn't work to upload a file in PHP - php

It doesn't work to upload a file. Please help me!

Form uploadfile.html
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
Select image upload
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" name="submit" value="Image Upload">
_File uploadfile.php
$upload_dir ="/uploads";
foreach ($_FILES["fileToUpload"]["error"] as $key => $error) {
if ($error ==UPLOAD_ERR_OK) {
$tmp_name=$_FILES["fileToUpload"]["tmp_name"][$key];
$name =basename($_FILES["fileToUpload"]["name"][$key]);
move_uploaded_file($tmp_name, "$upload_dir/$name");
}
}
_Don't enable upload any file.

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";
}
?>

where is the mistake in my script? [closed]

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).

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 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