sure (sorry for taking too long)
I have a form with six file upload things
HTML Code:
<input type='file' name='file[]' id='file1'>
<input type='file' name='file[]' id='file2'>
<input type='file' name='file[]' id='file3'>
<input type='file' name='file[]' id='file4'>
<input type='file' name='file[]' id='file5'>
<input type='file' name='file[]' id='file6'>
Then I'm trying to upload the files to the uploads directory using php, so I gather I first have to check if a file has even been uploaded
PHP Code:
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
echo "File ". $_FILES['file']['name'] ." uploaded successfully.\n";
echo "Displaying contents\n";
readfile($_FILES['file']['tmp_name']);
} else {
echo "Possible file upload attack: ";
echo "filename '". $_FILES['file']['tmp_name'] . "'.";
}
something like this
Here is the code to upload 1 file
PHP Code:
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
and i need to modify it to look though the files array to only run when needed.
But using the code you gave me
PHP Code:
function dump($dump_var) {
echo '<pre>';
var_dump($dump_var);
echo '</pre>';
}
function get_files($_FILES) {
$files=$_FILES['image'];
$file_count=count($files);
$count=0;
while ( $count <> $file_count-1 ) {
$sorted_files[$count]['name']=$files['name'][$count];
$sorted_files[$count]['type']=$files['type'][$count];
$sorted_files[$count]['tmp_name']=$files['tmp_name'][$count];
$sorted_files[$count]['error']=$files['error'][$count];
$sorted_files[$count]['size']=$files['size'][$count];
if (!is_uploaded_file($sorted_files[$count]['tmp_name'])) {
unset($sorted_files[$count]);
}
$count++;
}
return $sorted_files;
}
echo '<p>The files array before sorting';
dump($_FILES);
echo '</p>';
$the_files=get_files($_FILES);
echo '<p>The files after sorting';
dump($the_files);
echo '</p>';
produced (source code)
HTML Code:
<p>The files array before sorting<pre>array(1) {
["file"]=>
array(5) {
["name"]=>
array(6) {
[0]=>
string(8) "logo.gif"
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(8) "shop.png"
[5]=>
string(0) ""
}
["type"]=>
array(6) {
[0]=>
string(9) "image/gif"
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(9) "image/png"
[5]=>
string(0) ""
}
["tmp_name"]=>
array(6) {
[0]=>
string(14) "/tmp/phpxTCzY2"
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(14) "/tmp/phpT4bvGm"
[5]=>
string(0) ""
}
["error"]=>
array(6) {
[0]=>
int(0)
[1]=>
int(4)
[2]=>
int(4)
[3]=>
int(4)
[4]=>
int(0)
[5]=>
int(4)
}
["size"]=>
array(6) {
[0]=>
int(6258)
[1]=>
int(0)
[2]=>
int(0)
[3]=>
int(0)
[4]=>
int(53532)
[5]=>
int(0)
}
}
}
</pre></p><br />
<b>Fatal error</b>: Maximum execution time of 30 seconds exceeded in <b>/home/fixmy1/public_html/masterasp/providers/thanks.php</b> on line <b>60</b><br />
Can you show me how I screwed things up?
Bookmarks