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\">";
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!||||
//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 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 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.
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.
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 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.