LMS16
09-11-2010, 11:37 AM
Oh my, I look like an amateur... (I am rlly ;))
anyway, I'm making a multi-upload image script, yet the loop work, the only thing is, it only copies the first image....
Code:
define ("MAX_SIZE","51200"); // 50mb
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension2($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['upload_media2']))
{
for($x=0;$x<51;$x++){
$album = $_POST['albums'];
if(empty($album)){
$album = "../_imgs/_albums/";
$album2 = "0";
}else{
$album = "../_imgs/_albums/{$album}";
$get_alb = mysql_fetch_object(mysql_query("SELECT * FROM `albums` WHERE `name`='{$album}'"));
$album2 = $get_alb->id;
}
if(!is_dir($album)){
str_replace("/", "", $album);
mkdir($album);
chmod($album, 0777);
}
//reads the name of the file the user submitted for uploading
$image=$_FILES['image' . $x]['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image' . $x]['name']);
//get the extension of the file in a lower case format
$extension = getExtension2($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image' . $x]['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname=$album . "/" . $image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image' . $x]['tmp_name'], $newname);
chmod($newname, 0777);
mysql_query("INSERT INTO `imgs` (`img_link`, `alb`) VALUES ('{$newname}', '{$album2}')");
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}}
//If no errors registred, print the success message
if(isset($_POST['upload_media2']) && !$errors)
{
if(isset($_SERVER['HTTP_REFERER'])){
echo "<meta http-equiv=\"refresh\" content=\"0;url={$_SERVER['HTTP_REFERER']}\">";
}elseif($album != ""){
$alit = mysql_fetch_object(mysql_query("SELECT * FROM `albums` WHERE `name`='{$album}'"));
echo "<meta http-equiv=\"refresh\" content=\"0;url=?media&viewalb&id={$alit->id}\">";
}elseif(empty($album)){
echo "<meta http-equiv=\"refresh\" content=\"0;url=?media&manage\">";
}
}
?>
I know its messily coded, Im still working on it.
+REP
Lew.
anyway, I'm making a multi-upload image script, yet the loop work, the only thing is, it only copies the first image....
Code:
define ("MAX_SIZE","51200"); // 50mb
//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension2($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['upload_media2']))
{
for($x=0;$x<51;$x++){
$album = $_POST['albums'];
if(empty($album)){
$album = "../_imgs/_albums/";
$album2 = "0";
}else{
$album = "../_imgs/_albums/{$album}";
$get_alb = mysql_fetch_object(mysql_query("SELECT * FROM `albums` WHERE `name`='{$album}'"));
$album2 = $get_alb->id;
}
if(!is_dir($album)){
str_replace("/", "", $album);
mkdir($album);
chmod($album, 0777);
}
//reads the name of the file the user submitted for uploading
$image=$_FILES['image' . $x]['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image' . $x]['name']);
//get the extension of the file in a lower case format
$extension = getExtension2($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image' . $x]['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname=$album . "/" . $image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image' . $x]['tmp_name'], $newname);
chmod($newname, 0777);
mysql_query("INSERT INTO `imgs` (`img_link`, `alb`) VALUES ('{$newname}', '{$album2}')");
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}}
//If no errors registred, print the success message
if(isset($_POST['upload_media2']) && !$errors)
{
if(isset($_SERVER['HTTP_REFERER'])){
echo "<meta http-equiv=\"refresh\" content=\"0;url={$_SERVER['HTTP_REFERER']}\">";
}elseif($album != ""){
$alit = mysql_fetch_object(mysql_query("SELECT * FROM `albums` WHERE `name`='{$album}'"));
echo "<meta http-equiv=\"refresh\" content=\"0;url=?media&viewalb&id={$alit->id}\">";
}elseif(empty($album)){
echo "<meta http-equiv=\"refresh\" content=\"0;url=?media&manage\">";
}
}
?>
I know its messily coded, Im still working on it.
+REP
Lew.