473,390 Members | 1,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Uploading multiple files to server & MySQL

w33nie
56
What I'm trying to do here, is upload a video to the ../video/ folder, and up to 5 images to the ../images/ folder. As well as the database information like title, content and each file's file information.
But I really have no idea how to do this. The whole move_uploaded_file part is obviously wrong, but I'm not sure where to go from here to achieve my ends.

[php]
$videoUploadDir = '../video/';
$imageUploadDir = '../images/';

if(isset($_POST['upload']))
{
$title = $_POST['title'];
$content = $_POST['content'];
$image_number = $_POST['image_number'];
$imageName1 = $_FILES['image1']['name'];
$tmpImageName1 = $_FILES['image1']['tmp_name'];
$imageName2 = $_FILES['image2']['name'];
$tmpImageName2 = $_FILES['image2']['tmp_name'];
$imageName3 = $_FILES['image3']['name'];
$tmpImageName3 = $_FILES['image3']['tmp_name'];
$imageName4 = $_FILES['image4']['name'];
$tmpImageName4 = $_FILES['image4']['tmp_name'];
$imageName5 = $_FILES['image5']['name'];
$tmpImageName5 = $_FILES['image5']['tmp_name'];
$fileName = $_FILES['video']['name'];
$tmpName = $_FILES['video']['tmp_name'];
$fileSize = $_FILES['video']['size'];
$fileType = $_FILES['video']['type'];

// the files will be saved in filePath
$filePath = $videoUploadDir . $fileName;
$imagePath1 = $imageUploadDir . $imageName1;
$imagePath2 = $imageUploadDir . $imageName2;
$imagePath3 = $imageUploadDir . $imageName3;
$imagePath4 = $imageUploadDir . $imageName4;
$imagePath5 = $imageUploadDir . $imageName5;
$imageName1 = 'images/'. $imageName1;
$imageName2 = 'images/'. $imageName2;
$imageName3 = 'images/'. $imageName3;
$imageName4 = 'images/'. $imageName4;
$imageName5 = 'images/'. $imageName5;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = (move_uploaded_file($tmpName, $filePath) && ($tmpImageName1, $imagePath1) && ($tmpImageName2, $imagePath2) && ($tmpImageName3, $imagePath3) && ($tmpImageName4, $imagePath4) && ($tmpImageName5, $imagePath5));
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO video (title, content, image_number, image1, image2, image3, image4, image5, file_name, file_size, file_type, file_path)".
"VALUES ('$title', '$content', '$image_number', '$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5', '$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "SUCCESS";
}
[/php]
Sep 24 '07 #1
14 4446
pbmods
5,821 Expert 4TB
Heya, w33nie.

You'll want to call move_uploaded_file() once for each file:
Expand|Select|Wrap|Line Numbers
  1. if
  2. (
  3.     !
  4.     (
  5.             move_uploaded_file( $tmpName, $filePath )
  6.         &&
  7.             move_uploaded_file( $tmpName2, $filePath2 )
  8.         &&
  9.         .
  10.         .
  11.         .
  12.     )
  13. )
  14. {
  15.     echo ' ... ';
  16.     exit;
  17. }
  18.  
Sep 24 '07 #2
w33nie
56
Okay, tried that but when I click Upload, it came back with 'Error uploading file'

[php]
$videoUploadDir = '../video/';
$imageUploadDir = '../images/';

if(isset($_POST['upload']))
{
$title = $_POST['title'];
$content = $_POST['content'];
$image_number = $_POST['image_number'];
$imageName1 = $_FILES['image1']['name'];
$tmpImageName1 = $_FILES['image1']['tmp_name'];
$imageName2 = $_FILES['image2']['name'];
$tmpImageName2 = $_FILES['image2']['tmp_name'];
$imageName3 = $_FILES['image3']['name'];
$tmpImageName3 = $_FILES['image3']['tmp_name'];
$imageName4 = $_FILES['image4']['name'];
$tmpImageName4 = $_FILES['image4']['tmp_name'];
$imageName5 = $_FILES['image5']['name'];
$tmpImageName5 = $_FILES['image5']['tmp_name'];
$fileName = $_FILES['video']['name'];
$tmpName = $_FILES['video']['tmp_name'];
$fileSize = $_FILES['video']['size'];
$fileType = $_FILES['video']['type'];

// the files will be saved in filePath
$filePath = $videoUploadDir . $fileName;
$imagePath1 = $imageUploadDir . $imageName1;
$imagePath2 = $imageUploadDir . $imageName2;
$imagePath3 = $imageUploadDir . $imageName3;
$imagePath4 = $imageUploadDir . $imageName4;
$imagePath5 = $imageUploadDir . $imageName5;
$imageName1 = 'images/'. $imageName1;
$imageName2 = 'images/'. $imageName2;
$imageName3 = 'images/'. $imageName3;
$imageName4 = 'images/'. $imageName4;
$imageName5 = 'images/'. $imageName5;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false 1.
if (!( move_uploaded_file($tmpName, $filePath) && move_uploaded_file($tmpImageName1, $imagePath1) && move_uploaded_file($tmpImageName2, $imagePath2) && move_uploaded_file($tmpImageName3, $imagePath3) && move_uploaded_file($tmpImageName4, $imagePath4) && move_uploaded_file($tmpImageName5, $imagePath5) ))
{
echo 'Error uploading file';
exit;
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO video (title, content, image_number, image1, image2, image3, image4, image5, file_name, file_size, file_type, file_path)".
"VALUES ('$title', '$content', '$image_number', '$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5', '$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "SUCCESS";
}
[/php]
Sep 25 '07 #3
pbmods
5,821 Expert 4TB
Heya, w33nie.

Got anything in your error log?

Check out this article to find out what is going on.
Sep 25 '07 #4
w33nie
56
Apologies for not doing that in the first place.
I'm not seeing anything different with that on now though.
Sep 25 '07 #5
w33nie
56
Anyone know of a solution?
Sep 26 '07 #6
pbmods
5,821 Expert 4TB
Heya, w33nie.

Try doing one at a time.

Expand|Select|Wrap|Line Numbers
  1. if( ! move_uploaded_file($tmpName, $filePath) )
  2. {
  3.     echo 'Unable to move ', $tmpName, ' to ', $filePath, '.';
  4.     exit;
  5. }
  6.  
  7. if( ! move_uploaded_file($tmpImageName1, $imagePath1) )
  8. {
  9.     echo 'Unable to move ', $tmpImageName1, ' to ', $imagePath1, '.';
  10.     exit;
  11. }
  12.  
and so on.
Sep 26 '07 #7
w33nie
56
Yeah, when I uploaded 1 image, and it came up with "Unable to move to ../images/."

But when I uploaded 5, it all worked.
So how would I then prevent this from happening when I don't upload all 5, because I wont be nearly every time.
Sep 26 '07 #8
pbmods
5,821 Expert 4TB
Heya, w33nie.

Check for a valid filename before moving the file:
Expand|Select|Wrap|Line Numbers
  1. if( file_exists($tmpImageName1) )
  2. {
  3.     move_uploaded_file($tmpImageName1, $imagePath1);
  4. }
  5.  
Sep 27 '07 #9
w33nie
56
Works perfectly, thanks

Here's the finished php for anyone interested,

[php]
$videoUploadDir = '../video/';
$imageUploadDir = '../images/';

if(isset($_POST['upload']))
{
$title = $_POST['title'];
$content = $_POST['content'];
$image_number = $_POST['image_number'];
$imageName1 = $_FILES['image1']['name'];
$tmpImageName1 = $_FILES['image1']['tmp_name'];
$imageName2 = $_FILES['image2']['name'];
$tmpImageName2 = $_FILES['image2']['tmp_name'];
$imageName3 = $_FILES['image3']['name'];
$tmpImageName3 = $_FILES['image3']['tmp_name'];
$imageName4 = $_FILES['image4']['name'];
$tmpImageName4 = $_FILES['image4']['tmp_name'];
$imageName5 = $_FILES['image5']['name'];
$tmpImageName5 = $_FILES['image5']['tmp_name'];
$fileName = $_FILES['video']['name'];
$tmpName = $_FILES['video']['tmp_name'];
$fileSize = $_FILES['video']['size'];
$fileType = $_FILES['video']['type'];

// the files will be saved in filePath
$filePath = $videoUploadDir . $fileName;
$imagePath1 = $imageUploadDir . $imageName1;
$imagePath2 = $imageUploadDir . $imageName2;
$imagePath3 = $imageUploadDir . $imageName3;
$imagePath4 = $imageUploadDir . $imageName4;
$imagePath5 = $imageUploadDir . $imageName5;
$imageName1 = 'images/'. $imageName1;
$imageName2 = 'images/'. $imageName2;
$imageName3 = 'images/'. $imageName3;
$imageName4 = 'images/'. $imageName4;
$imageName5 = 'images/'. $imageName5;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
if( file_exists($tmpName) ){
move_uploaded_file($tmpName, $filePath);
}
if( file_exists($tmpImageName1) ){
move_uploaded_file($tmpImageName1, $imagePath1);
}
if( file_exists($tmpImageName2) ){
move_uploaded_file($tmpImageName2, $imagePath2);
}
if( file_exists($tmpImageName3) ){
move_uploaded_file($tmpImageName3, $imagePath3);
}
if( file_exists($tmpImageName4) ){
move_uploaded_file($tmpImageName4, $imagePath4);
}
if( file_exists($tmpImageName5) ){
move_uploaded_file($tmpImageName5, $imagePath5);
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO video (title, content, image_number, image1, image2, image3, image4, image5, file_name, file_size, file_type, file_path)".
"VALUES ('$title', '$content', '$image_number', '$imageName1', '$imageName2', '$imageName3', '$imageName4', '$imageName5', '$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('<hr />Error, query failed : ' . mysql_error(). '<hr />');

echo "<h4>Article uploaded</h4><br />";
}
[/php]
Sep 27 '07 #10
pbmods
5,821 Expert 4TB
Heya, w33nie.

Thanks for posting your solution.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 27 '07 #11
daav31
4
Heya, w33nie.

Thanks for posting your solution.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Hi:

I have a problem along the same lines, but more specifically I am trying to do a loop to determine obatain the files individually that were uploaded to a directory, unlike the previous situation where each $_FILES is manually inserted into a different variable. I am not sure how I should be able to loop through $_FILES['Filedata'] and subsequently $_FILES['Filedata'] ['temp_name'] and $_FILES['Filedata'] ['name']

beginning with:

[PHP]
if ($_REQUEST['uploadDir']){
if(isset($_FILES['Filedata']) && is_array($_FILES['Filedata']) && isset ($_FILES['Filedata']['tmp_name']
,$_FILES['Filedata']['name'], $_FILES['Filedata']['size'], $_FILES['Filedata']['error']) && intVal(
$_FILES['Filedata']['error']) === 0) {
if(move_uploaded_file($_FILES['Filedata']['tmp_name'], $upld_dir.$_FILES['Filedata']['name'])) {
$result = "Date: ".date('Y-m-d H:i:s')." File: {$_FILES['Filedata']['name']} Size:
{$_FILES['Filedata']['size']} Successfull uploaded.";
} else {
$result = "Date: ".date('Y-m-d H:i:s')."File: {$_FILES['Filedata']['name']}Size:
{$_FILES['Filedata']['size']}Error: {$_FILES['Filedata']['error']}Unable to move file.";
}
[/PHP]


Thanks much for any help...
Dec 7 '07 #12
pbmods
5,821 Expert 4TB
Heya, Daav. Welcome to TSDN!

What do you get if you print_r($_FILES)?
Dec 7 '07 #13
daav31
4
Heya, Daav. Welcome to TSDN!

What do you get if you print_r($_FILES)?
Thanx for the prompt response,
I get [PHP]Array()[/PHP]

But I should also mention that the files are initially uploaded all at once via a multiple file upload flash tool into the directory uploadDir through the script I posted. Ultimately, I want to put each uploaded file as a separate record in the database, but I first can't figure out how to process each uploaded file separately with a loop (using foreach or a for loop) in the php script.
Dec 7 '07 #14
pbmods
5,821 Expert 4TB
Heya, Daav.

Looks like no files are getting uploaded.

The usual candidate: Did you remember to add the enctype attribute to your form?

Expand|Select|Wrap|Line Numbers
  1. <form method="post" enctype="multipart/form-data" action="...">
  2.  
Dec 8 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sean peters | last post by:
Hi all, i've been weighing the pros and cons of running multiple concurrent mysqld's on one server, to have better control over what databases are on what physical disks. System: 4 processor sun...
1
by: Andy | last post by:
I'm using Mysql Max-4.1.4-gamma for Linux. I have a question related to a problem with my database server. If on my db server I have 2 different database, say DB1 & DB2, I create two INNODB tables...
4
by: MLH | last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application for me. When he was done, he sent me the PHP files and the MySQL dump file. Now, when I connect to the application on my LAN using...
5
by: Ron Brennan | last post by:
Good afternoon. The entire task that I'm trying to achieve is to allow a user to browse and upload multiple files simultaneously, hiding the Browse button of <input> tags of type="file" and...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
4
by: Corey Erkes | last post by:
I am using ASP.Net with C# as the code behind and am trying to upload multiple files with the HTMLInputFile Control. I am able to browse, select a file, and finally upload, but only one file at a...
2
by: tshad | last post by:
I have a page that I am using for multiple uses that includes reposting of the page as well as going to another page where the "form" tag would be something like: <form id="something"...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.