By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

Uploading multiple files to server & MySQL

w33nie
P: 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
Share this Question
Share on Google+
14 Replies


pbmods
Expert 5K+
P: 5,821
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
P: 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
Expert 5K+
P: 5,821
Heya, w33nie.

Got anything in your error log?

Check out this article to find out what is going on.
Sep 25 '07 #4

w33nie
P: 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
P: 56
Anyone know of a solution?
Sep 26 '07 #6

pbmods
Expert 5K+
P: 5,821
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
P: 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
Expert 5K+
P: 5,821
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
P: 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
Expert 5K+
P: 5,821
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

P: 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
Expert 5K+
P: 5,821
Heya, Daav. Welcome to TSDN!

What do you get if you print_r($_FILES)?
Dec 7 '07 #13

P: 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
Expert 5K+
P: 5,821
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

Post your reply

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