PDA

View Full Version : ANOTHER PHP question...



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.

Dentafrice
09-11-2010, 09:26 PM
What's the deal in repeating it 51 times?

MattFr
09-11-2010, 09:29 PM
}}}}}


lol, tidy the code up and I will help, your code is stupidly messy

LMS16
10-11-2010, 08:26 AM
}}}}}


lol, tidy the code up and I will help, your code is stupidly messy

Hence why I said "I know its messily coded, Im still working on it."

And I am repeating it 51 times as it is a multi-upload script with 51 upload boxes... :/

Help?

Lew.

Dentafrice
10-11-2010, 12:19 PM
What is your HTML code for the form..

Moh
10-11-2010, 02:59 PM
What's the possibility of somebody uploading 51 images? Doing it 51 times without any need will just drain your server resources. Would be best to maybe add a bit of javascript that adds a file upload field when needed and then submit the total amount of elements in use.

LMS16
10-11-2010, 04:54 PM
What's the possibility of somebody uploading 51 images? Doing it 51 times without any need will just drain your server resources. Would be best to maybe add a bit of javascript that adds a file upload field when needed and then submit the total amount of elements in use.

Yes possibly, but I need this to work for the moment...

Lew.

Dentafrice
10-11-2010, 09:56 PM
Post your HTML for your upload form..

LMS16
11-11-2010, 07:56 AM
<form name="newad2" method="post" enctype="multipart/form-data" action="">
<input name="albums" type="hidden" value="<?php echo $alb_info1->name; ?>" />

<select name="albums">
<option value="">No album</option>
<?php $album = mysql_query("SELECT * FROM `albums`");
while($val4 = mysql_fetch_object($album)){
echo "<option value=\"{$val4->name}\">{$val4->name}</option>";
}
?>
</select>
<?php } ?>
<hr />
<?php for($t=0;$t<51;$t++){
echo "<input type=\"file\" name=\"image{$t}\" id=\"image{$t}\">";
}
?>
<input name="upload_media2" type="submit" value="Upload image">
</form>

Dentafrice
11-11-2010, 03:28 PM
That's not the way you do it there bud... never has worked for me... you need to name the file something with brackets... userfile[].

It is then put into an array.. $_FILES['userfile]['name'][0]. I suggest you read this tutorial: http://www.phpeasystep.com/phptu/2.html

LMS16
12-11-2010, 10:07 AM
That's not the way you do it there bud... never has worked for me... you need to name the file something with brackets... userfile[].

It is then put into an array.. $_FILES['userfile]['name'][0]. I suggest you read this tutorial: http://www.phpeasystep.com/phptu/2.html


Thanks, Ill have a look at that :) +REP

Want to hide these adverts? Register an account for free!