Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-01-2012, 03:12 AM   PM User | #1
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
RESOLVED Multi upload script

Can anyone please help figure out how to get the 3 upload names out of the array and into a mysql database.?

or is there a better way of doing this altogether?


PHP Code:
<?php
//set where you want to store files
//in this example we keep file in folder upload 
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1"../files/".$HTTP_POST_FILES['ufile']['name'][0];
$path2"../files/".$HTTP_POST_FILES['ufile']['name'][1];
$path3"../files/".$HTTP_POST_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);


//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"
echo 
"File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>"
echo 
"File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"
echo 
"<img src=\"$path1\" width=\"150\" height=\"150\">";
echo 
"<P>";

echo 
"File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"
echo 
"File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo 
"File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"
echo 
"<img src=\"$path2\" width=\"150\" height=\"150\">";
echo 
"<P>";

echo 
"File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"
echo 
"File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo 
"File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"
echo 
"<img src=\"$path3\" width=\"150\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];

if(
$filesize1 && $filesize2 && $filesize3 != 0)
{
echo 
"We have recieved your files";
}

else {
echo 
"ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo 
"There're something error in your first file";
echo 
"<BR />";
}

if(
$filesize2==0) {
echo 
"There're something error in your second file";
echo 
"<BR />";
}

if(
$filesize3==0) {
echo 
"There're something error in your third file";
echo 
"<BR />";
}

}

?>
Thanks in advance.

Last edited by SlayerACC; 05-01-2012 at 02:47 PM..
SlayerACC is offline   Reply With Quote
Old 05-01-2012, 04:51 AM   PM User | #2
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Nevermind...

I figured it out... shock of shocks.


Call a parade..


Thanks anyway.


Slayer.
SlayerACC is offline   Reply With Quote
Old 05-01-2012, 04:53 AM   PM User | #3
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
What was your solution? Did it involve some kind of loop? This is how I would have done it.
PHP Code:
<?php 
$errors 
= array();
foreach(
$_FILES["ufile"]["error"] as $key => $error)
{
    if (
$error == UPLOAD_ERR_OK)
    {
        
// common variables local to this loop
        
$filename $_FILES['ufile']['name'][$key];
        
$filesize $_FILES['ufile']['size'][$key];
        
$filetype $_FILES['ufile']['type'][$key];
        
$path "../files/".$filename;
        
        
// does the file have a filesize? If so attempty to upload it
        
if(move_uploaded_file($_FILES['ufile']['tmp_name'][$key], $path))
        {
            echo 
"File Name :".$filename."<br/>";  
            echo 
"File Size :".$filesize."<br/>";  
            echo 
"File Type :".$filetype."<br/>";  
            echo 
"<img src=\"$path\" width=\"150\" height=\"150\"><br/>";
        }
        else 
// there was a problem with a file, add the problem file to errors array
        
{
            
$errors[] = $filename." was not received.<br>";    
        }
    }
}

if(
count($errors) == 0// if the errors array size is 0 all files were uploaded.

    echo 
"We have recieved your files"

else 
// echo out the files that had problems

    foreach(
$errors as $error)
    {
        echo 
$error;    
    }

?>
move_uploaded_file is better to use as it does some validation to be sure it was a valid file upload.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 05-01-2012, 01:22 PM   PM User | #4
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
However not as pretty as yours.. thanks by the way... very clean.

this is what got mine to work,

PHP Code:
<?php

$path1
"../files/".$HTTP_POST_FILES['ufile']['name'][0];
$path2"../files/".$HTTP_POST_FILES['ufile']['name'][1];
$path3"../files/".$HTTP_POST_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);



function 
reArrayFiles(&$file_post) {

    
$file_ary = array();
    
$file_count count($file_post['name']);
    
$file_keys array_keys($file_post);

    for (
$i=0$i<$file_count$i++) {
        foreach (
$file_keys as $key) {
            
$file_ary[$i][$key] = $file_post[$key][$i];
        }
    }

    return 
$file_ary;
}

if (
$HTTP_POST_FILES['ufile']['name']) {
    
$file_ary reArrayFiles($_FILES['ufile']);

    foreach (
$file_ary as $file) {
        print 
'File Name: ' $file['name'];
        echo 
"<br/>";
        print 
'File Type: ' $file['type'];
        echo 
"<br/>";
        print 
'File Size: ' $file['size'];
        echo 
"<br/>";
        print 
"<img src=\"../files/".$file['name']."\" width=\"150\" height=\"150\">";
        echo 
"<br/>";
    }
}

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];

if(
$filesize1 && $filesize2 && $filesize3 != 0)
{
echo 
"We have recieved your files";

$array $HTTP_POST_FILES['ufile']['name'];
foreach(
$array as $files)
{
  if (
strlen($files)>0)
 {
$con mysql_connect("localhost","root","pass");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }
mysql_select_db("database"$con);$sql="INSERT INTO `files` (`files_id`, `filename`)
VALUES
('$_POST[files_id]','$files')"
;if (!mysql_query($sql,$con))
  {
  die(
'Error: ' mysql_error());
  }
echo 
"1 record added";
print 
"<br><br><a href=../index.php>back</a>";
mysql_close($con);

 }
}
}

else {
echo 
"ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo 
"There're something error in your first file";
echo 
"<BR />";
}

if(
$filesize2==0) {
echo 
"There're something error in your second file";
echo 
"<BR />";
}

if(
$filesize3==0) {
echo 
"There're something error in your third file";
echo 
"<BR />";
}



?>
Now to try to figure out how to add a button to create 10 browse boxes.. or one at a time dynamically.


Slayer.

Last edited by SlayerACC; 05-01-2012 at 01:28 PM..
SlayerACC is offline   Reply With Quote
Old 05-01-2012, 03:00 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
$HTTP_POST_FILES is deprecated - OLD, very old. You shouldn't be using it as it's rumoured that it will be withdrawn in future versions of PHP.

Instead you should be using a different superglobal called $_FILES instead. If you don't and the servers PHP version is upgraded with a version that no longer supports $HTTP_POST_FILES then your code will stop working.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
SlayerACC (05-01-2012)
Old 05-01-2012, 03:13 PM   PM User | #6
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
That is great info.. I would have been mad if it stopped working.


Thanks Tango...

I will change some things around....

Slayer.
SlayerACC is offline   Reply With Quote
Old 05-01-2012, 08:57 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,449 Times in 2,418 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
To further expand, its currently disable-able. Registered long arrays are a directive that I believe was introduced in 5.0, and is now gone as of 5.4. I can test it when I get home tonight, but I'm betting that the RAW is still available since there is no superglobal for it anyway (although it'd probably be better to pull from php://input instead), but the rest from get, post, cookie, files, etc are all gone.

So in other words, if you leave it as HTTP_POST_FILES, I'd expect that it'd fail to work much sooner than later.
Fou-Lu is offline   Reply With Quote
Old 05-01-2012, 11:00 PM   PM User | #8
SlayerACC
Regular Coder

 
Join Date: Sep 2009
Location: Calgary, Alberta
Posts: 222
Thanks: 45
Thanked 3 Times in 3 Posts
SlayerACC is an unknown quantity at this point
Thanks all,


You are great as usual ... I would love to know how you guys seem to know everything..

>>

anyway thanks.
SlayerACC is offline   Reply With Quote
Old 05-02-2012, 12:34 AM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by SlayerACC View Post
You are great as usual ... I would love to know how you guys seem to know everything..
3 key ingredients:

1) Time
2) Headscratching*
3) Experimenting
4) Learning to debug - theorise what might be happening and then find a way to test it somehow to see if you're right or wrong.

Thats all there is to it

* = Use of php.net to lookup what functions do, google to get inspiration for writing code and tizag.com for help with other things such as mysql
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:15 AM.


Advertisement
Log in to turn off these ads.