File Upload Utility using PHP - php

I have a webpage I'm building to allow users to upload multiple files to my webserver. Its a simple html page and php script. I'm having php errors and i'm not sure why. Any help is greatly appreciated.
Here is my html.
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Your Name:
<select name="uname">
<option value="Test">Test</option>
</select>
<br>
Your Company:
<select name="company">
<option value="TestCom">testCompany</option>
</select>
<br>
Choose file(s) to upload (Max 500MB):
<input name="files[]" type="file" id="files" multiple="multiple" />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
and here is my php
<?php
$uname = $_POST['uname'];
$company = $_POST['company'];
$dir = "D:/File Upload/uploads/$uname/$company/";
$count = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files']))
{
// loop all files
foreach ( $_FILES['files']['name'] as $i => $name )
{
// if file not uploaded then skip it
if ( !is_uploaded_file($_FILES['files']['tmp_name'][$i]) )
continue;
// now we can move uploaded files
if( move_uploaded_file($_FILES["files"]["tmp_name"][$i], $dir . $name) )
$count++;
}
echo json_encode(array('count' => $count));
}
?>
Here is the errors in the php log.
[11-Feb-2015 16:01:04 America/Chicago] PHP Warning: move_uploaded_file(D:/File Upload/uploads/Test/TestCom/image.png): failed to open stream: No such file or directory in D:\File Upload\upload.php on line 17
[11-Feb-2015 16:01:04 America/Chicago] PHP Warning: move_uploaded_file(): Unable to move 'D:\phptempdata\php48F.tmp' to 'D:/File Upload/uploads/Test/TestCom/image.png' in D:\File Upload\upload.php on line 17

Turns out I didn't have the directories specified actually created on the files system.
I had been working so hard on just getting the file uploaded, I don't stop to ask if the folder was actually there.
I added this line to create the folder if it didn't exist.
if (!file_exists($dir)) { mkdir($dir, 0777, true); }

Related

PHP image upload - several errors

So, I'm trying to upload an image using move_uploaded_file(). Here is the code:
HTML FORM:
<form method="post" action="?page=prod&action=register" enctype="multipart/form-data">
<fieldset class="field-prods">
<label>Name:</label> <br>
<input type="text" name="txtName">
</fieldset>
<fieldset class="field-prods">
<label>Price:</label> <br>
<input type="number" name="nmbPrice">
</fieldset>
<fieldset class="field-prods">
<label>Image:</label> <br>
<input type="file" name="fileImage">
</fieldset>
<button type="submit" class="btn-prods">Register</button>
</form>
PHP SCRIPT:
if ($_POST != null) {
if (!empty($_POST['txtName']) && !empty($_FILES['fileImage']) && !empty($_POST['nmbPrice'])) {
Product::insert($_POST['txtName'], $_FILES['fileImage'], $_POST['nmbPrice']);
$target = "img/prods/" . $_FILES['fileImage']['name'];
$fileTmpName = $_FILES['fileImage']['tmp_name'];
move_uploaded_file($fileTmpName, $target);
}
}
But I'm getting lot of warnings:
*Warning: Array to string conversion*
*Warning: move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: No such file or directory*
*Warning: move_uploaded_file(): Unable to move "F:\Programs\Xampp\tmp\php7CE5.tmp" to "img/prods/livraria-print1.jpg"*
Can someone please help me with that?
# Array to string conversion
This warning is in insert function: you passed array ($_FILES['fileImage']) instead of string.
It seems that you want to store address of image, if that so it is better to store the image first then try to save the address of image.
# move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: ...
This warning is clear. check your directories and sure that the path you used is correct.
# move_uploaded_file(): Unable to move
And this one is because of prev warning.

in upload form php beginner programming

I have made an upload form and PHP code to save file upload
HTML:
<HTML>
<head>
<title></title>
</head>
<body>
<form action='upload.php'method="post"enctype="multipart/formdata">
<input type="file" name="file" size="100000" />
<Br />
<input type="submit" value="Upload File" />
</form>
</body>
</HTML>
PHP:
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], __DIR__ ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
</body>
</html>
I've changed my code to the form of tutorialspoint php pdf,but
still have a problem:
Warning: copy(111.jpg): failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\upload.php on line 4
The target path is incorrect. If you want to move dynamically the file to a "upload" directory inside your main directory, you can change :
if(move_uploaded_file($_FILES['file']['name'],".;C:\ProgramFiles\EasyPHPDevServer-14.1VC9\data\localweb"))
to
if (move_uploaded_file($_FILES["file"]["name"], __DIR__ . "/upload/" . basename($_FILES["file"]["name"]))) {
tnx all friend help my solve this.Ive done it by this script:
http://www.w3schools.com/php/php_file_upload.asp

File not being uploaded php

UPDATE
I have now solved it, the problem was that I did not have the preciding '/' and I did not have the '/' at the end, here is the final syntax of the folder path: $folder = '/home/dl/www/uploads/';
UPDATE
I have updated the code so that is can output some more detailed debugging information, and this is what is output:
Warning: move_uploaded_file(home/dl/www/uploads/test.txt): failed to open stream: No such file or directory in /home/dl/public_html/file-upload/upload.php on line 9
I have the below script which is attempting to upload a selected file to a specified directory on my web server. However, it is not uploading; I have checked that the permissions on the directory is at '777' as per numerous tutorials have suggested, but it is still throwing the 'File is not uploaded' message.
Does anyone have any suggestions as to why this may not be working?
Thanks!
<?php
$folder = "/home/dl/www/uploads";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
Echo "File uploaded";
} else {
Echo "File not moved to destination folder. Check permissions";
};
} else {
Echo "File is not uploaded.";
};
?>
The HTML form:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Try
$folder = "/home/dl/www/uploads/";
instead of
$folder = "/home/dl/www/uploads";
That is, a slash at the end.

warning message shows during the file upload using php

I am using ubuntu os with XAMPP. Here i've created two files uploader.php and phpEx6.php
when i try to upload a file it shows warning message.I am new to php.pls help to solve the problem..
phpEx6.php
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="uploader.php" method="post" enctype="multipart/form- data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
uploader.php
<?php
if( $_FILES['file']['name'] != "" )
{
error_reporting(E_ALL);
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/' $_FILES['file']['name'])
or die("Couldn't copy the file.");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>enter code here
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
OUTPUT
Warning: move_uploaded_file(/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt): failed to open stream: No such file or directory in /opt/lampp/htdocs/xampp/nf/uploader.php on line 5
Warning:
move_uploaded_file(): Unable to move '/opt/lampp/temp/php1BAGC1' to '/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt' in /opt/lampp/htdocs/xampp/nf/uploader.php on line 5
try altering this
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])
with this
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/xampp/nf/uploads/'. $_FILES['file']['name'])
or even this
move_uploaded_file($_FILES['file']['tmp_name'], '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])
The error is about, there's no folder created to which you are trying to move the files.. Create the folder and then try it or check whether the folder has all the permissions needed to upload the file into it..
Here's the sample code for Uploading a file..
if(isset($_POST['Submit']))
{
$tmp_name = $_FILES['Pic']['tmp_name'];
$name = $_FILES['Pic']['name'];
if(is_uploaded_file($tmp_name))
{
$org_name = time()."_".$name;
$dest = "uploads/$org_name";
move_uploaded_file($tmp_name, $dest);
}
}
there can be multiple answer for your questions as followed:
start path with DOT(.) such as './opt/foldername/'
create folder and give write permission to it in which you want to insert your file and data
or you can do before move uploaded in such as way just check first whether directory exist or not if its not than use mkdir function and than chmod to give write permission and than use upload function. its always a better practice to check directory existence and than only move the file there.
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/xampp/nf/uploads/'. $_FILES['file']['name'])
#Peter Darmis

Upload multiple text files into MySQL

I have ZERO experience coding uploading files through browser, so this part is all very new to me.
I need to give users (in fact they will be only one or two authorized users) a way to upload multiple text files (think 50-200 files) directly into a MYSQL database.
I don't want to give them FTP access, but am OK allowing them to enter files into the database.
I can figure out how to get the data from a PHP array into the MYSQL database.
What I can't figure out is how to get the contents of multiple files into the PHP array(s).
Please help out with the code.
This example should help you understand the basic idea
<?php
$fileContents = Array();
if(count($_FILES) != 0){
foreach($_FILES as $file){
$fp = fopen($file["tmp_name"], "r");
array_push($fileContents, fread($fp, $file["size"]));
fclose($fp);
}
//$fileContents now holds all of the text of every file uploaded
}
?>
<html>
<head>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file1" id="file" />
<input type="file" name="file2" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
It first checks that there has been files posted to itself.
If there is files, it loops through each and opens them while they are in their temporary file state.
After that it reads all of the contents at once (be careful with this) using the size attribute that sent with it.
At the same time, it is pushing the contents into the array called $fileContents.
So $fileContents[0] holds the first text file and so on.
Just add more <input type="file">s to your page, and they will all appear in the $_FILES array, which you can loop to retrieve them.
However:
The structure of the $_FILES array is slightly illogical when it comes to multiple files - make sure you read the manual carefully, it is a little counter intuitive.
Make sure that your upload_max_filesize, post_max_size, max_file_uploads and max_input_time PHP.ini directives are generous enough.
See also: Handling multiple file uploads in PHP.
<!-- FORM -->
<form method="post" enctype="multipart/form-data">
<?php
for($i=1;$i<=10;$i++) //change 10 to any number for more upload fields
{
echo '<input type="file" name="files[]" /><br />';
}
?>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
//Processor
if(isset($_POST['submit']))
{
foreach($_FILES['files']['tmp_name'] as $tmp_name)
{
if(!empty($tmp_name))
{
$filecontent[] = file_get_contents($tmp_name);
}
}
//Test
echo '<pre>';
print_r($filecontent);
echo '</pre>';
}
?>
Thank you everyone who has contributed to this. I am having a very hard time choosing the answer, because I think it's a 50/50 effort by John and DaveRandom.
In case someone wants to see the end product here it is:
HTML:
<html>
<head>
</head>
<body>
<form method="post" action="test.php" enctype="multipart/form-data">
<input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP:
<?php
function rearrange( $arr ){
foreach( $arr as $key => $all ){
foreach( $all as $i => $val ){
$new[$i][$key] = $val;
}
}
return $new;
}
$fileContents = Array();
if(count($_FILES['filesToUpload'])) {
$realfiles=rearrange($_FILES['filesToUpload']);
foreach ($realfiles as $file) {
$fp = fopen($file["tmp_name"], "r");
array_push($fileContents, fread($fp, $file["size"]));
fclose($fp);
}
foreach ($fileContents as $thisone) {
echo "<textarea wrap='off'>\n";
echo $thisone;
echo "</textarea>\n";
echo "<br>----<br>";
}
}
?>

Categories