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]
14 4278
Heya, w33nie.
You'll want to call move_uploaded_file() once for each file: -
if
-
(
-
!
-
(
-
move_uploaded_file( $tmpName, $filePath )
-
&&
-
move_uploaded_file( $tmpName2, $filePath2 )
-
&&
-
.
-
.
-
.
-
)
-
)
-
{
-
echo ' ... ';
-
exit;
-
}
-
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]
Heya, w33nie.
Got anything in your error log?
Check out this article to find out what is going on.
Apologies for not doing that in the first place.
I'm not seeing anything different with that on now though.
Anyone know of a solution?
Heya, w33nie.
Try doing one at a time. -
if( ! move_uploaded_file($tmpName, $filePath) )
-
{
-
echo 'Unable to move ', $tmpName, ' to ', $filePath, '.';
-
exit;
-
}
-
-
if( ! move_uploaded_file($tmpImageName1, $imagePath1) )
-
{
-
echo 'Unable to move ', $tmpImageName1, ' to ', $imagePath1, '.';
-
exit;
-
}
-
and so on.
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.
Heya, w33nie.
Check for a valid filename before moving the file: -
if( file_exists($tmpImageName1) )
-
{
-
move_uploaded_file($tmpImageName1, $imagePath1);
-
}
-
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]
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 :)
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...
Heya, Daav. Welcome to TSDN!
What do you get if you print_r($_FILES)?
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.
Heya, Daav.
Looks like no files are getting uploaded.
The usual candidate: Did you remember to add the enctype attribute to your form? -
<form method="post" enctype="multipart/form-data" action="...">
-
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by sean peters |
last post: by
|
1 post
views
Thread by Andy |
last post: by
|
4 posts
views
Thread by MLH |
last post: by
|
5 posts
views
Thread by Ron Brennan |
last post: by
|
13 posts
views
Thread by Sky Sigal |
last post: by
|
4 posts
views
Thread by Corey Erkes |
last post: by
|
2 posts
views
Thread by tshad |
last post: by
|
5 posts
views
Thread by Chris |
last post: by
| | | | | | | | | | | |