I've written this code that takes two variables via POST. Some file and a checkbox to say whether the image can be downloaded. I then have this processing code to upload multiple files:
<?php
require_once('Connections/koding1.php');
if(isset($_FILES['file']['tmp_name']))
{
// Number of uploaded files
$num_files = count($_FILES['file']['tmp_name']);
/** loop through the array of files ***/
for($i=0; $i < $num_files;$i++)
{
echo "Started loop";
// check if there is a file in the array
if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
{
echo "No file";
$messages[] = 'No file uploaded';
}
else
{
echo "Upload Loop";
$upload_dir = rand(1,5);
$rand_code = mt_rand(1,800);
$rand_string = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 15);
if ($_POST['imgdownloadable'] == "yes"){
$downloadable = "yes";
}else{
$downloadable = "no";
}
// copy the file to the specified dir
if(@copy($_FILES['file']['tmp_name'][$i],'uploads/' . $upload_dir. '/' . $_SESSION['MM_Username'] . $rand_code . $rand_string .$_FILES['file']['name'][$i] ))
{
$downloadlink = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5);
$filename = $_FILES['file']['name'][$i];
$mysqlstring = "INSERT INTO images (user, server, img-name, download, download-url) VALUES ($_SESSION[MM_Username], $upload_dir, $filename, $downloadable, $downloadlink";
$mysql = mysql_query($mysqlstring) or die(mysql_error());
$messages[] = $_FILES['file']['name'][$i].' uploaded';
}
else
{
echo "Failed";
/*** an error message ***/
$messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
}
}
}
}
?>
<?php echo $mysql;
echo '<pre>';
var_dump($messages);
echo '</pre>';
?>
The bit after echo "Upload Loop;
is a bit confusing so i'll explain it
Let's say we're logged in with a username of "example@example.com
Pick a random number between 1 and 5 , say 3
Pick another random number, say 15
Pick a random string, say xyz
If the checkbox is checked set downloadable to yes, else no.
Move our temporary upload (lets say it's called test.png) to uploads/3/example@example.com15xyztest.png
I've written some debug echos in and all I get back isNULL
Thanks for any help!