Jump to content

Counting files in multiple upload script


davelearning

Recommended Posts

Hi all,

 

I am having a few problems.

 

Basically I have a multiple file upload script, that I can get successfully to save the images to a folder. However what I am trying to do is enter a record in the database for every file that is uploaded, giving it a picture id, album id and user id (p_id, a_id, u_id). From here I then want to rename the image to the p_id in the database(auto_increment).

 

I am starting just by counting the amount of images uploaded and then creating a record. However I seem to have something wrong, as when I tried it to upload 1 image as a test it created 388,000 blank records! (just p_id as auto)

 

Can someone explain what I am doing wrong? I am quite new to php.

<?php


$result = array();

$result['time'] = date('r');
$result['addr'] = substr_replace(gethostbyaddr($_SERVER['REMOTE_ADDR']), '******', 0, 6);
$result['agent'] = $_SERVER['HTTP_USER_AGENT'];

if (count($_GET)) {
$result['get'] = $_GET;
}
if (count($_POST)) {
$result['post'] = $_POST;
}
if (count($_FILES)) {
$result['files'] = $_FILES;
}

// we kill an old file to keep the size small
if (file_exists('script.log') && filesize('script.log') > 102400) {
unlink('script.log');
}

$log = @fopen('script.log', 'a');
if ($log) {
fputs($log, print_r($result, true) . "\n---\n");
fclose($log);
}


// Validation

$error = false;

if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
$error = 'Invalid Upload';
}

else

{



include('../includes/config.php');
session_start();
$a_id = $_SESSION["a_id"];
$u_id = $_SESSION["id"];
$togo = $result['files'];
$i=0;
while ($i < $togo) 

{
$query = mysql_query("INSERT into images (a_id,u_id)
VALUES
('$a_id','$u_id')");

$i++;
}

}

?>

 

 

 

Many Thanks

 

 

$togo is a copy of your $result['files'] which is a copy of the $_FILES array. Comparing an integer with an array doesn't mean anything.

 

If you want to loop over an array of uploaded files, I recommend that you read example #3 at this link - http://us3.php.net/manual/en/features.file-upload.post-method.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.