Jump to content

Uploading multiple files - How to echo all details?


leettari

Recommended Posts

Hey! First timer here, please be gentle on me. ::)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Leettarin Uploader</title>
   //<link href="style.css" rel="stylesheet" type="text/css" />
   <BODY BACKGROUND="Ready.jpg">
   

<link rel="shortcut icon" href="xxx/twg/favicon.ico">
</head>
<?php

ob_start();

define("MAX_COUNT", 10);
define("UPLOAD_DIRECTORY", "valokuvat/");
define("MAX_SIZE", 1000000);

if(!is_dir(UPLOAD_DIRECTORY)) {
    mkdir(UPLOAD_DIRECTORY, 0777);
}

if(file_exists($_FILES['file1']['tmp_name'])) {
    for($i=1; $i<=MAX_COUNT; $i++) {

        if($_FILES['file'.$i]['size'] > MAX_SIZE) { echo "Liian iso tiedosto!<br>".MAX_SIZE." on raja"; break; }

        while(file_exists(UPLOAD_DIRECTORY.$_FILES['file'.$i]['name'])) {$_FILES['file'.$i]['name']="$k".$_FILES['file'.$i]['name']; $k++; }


        move_uploaded_file($_FILES['file'.$i]['tmp_name'], UPLOAD_DIRECTORY.$_FILES['file'.$i]['name']);
echo "xxx/waddap/valokuvat/".$_FILES['file'.$i]['name'];
exit();
    }
}
?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" ENCTYPE="multipart/form-data">
    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="102400000">
<?php
for($i=1;$i<=MAX_COUNT;$i++) {
    echo " <b><font style=\"color: #00FFFF;\">$i</font></b> <input type=\"file\" name=\"file".$i."\" size=\"75\"><br>";
}

?>    <input type="submit" value="Upload"></form><?php

ob_end_flush();
exit;
?>

 

Basically, I want this to also display details concerning EVERY uploaded file.

I can fix the text and decoration part later, but for now I'd like to know how to make it display information from every uploaded file upon multi-uploading.

 

Information, as in a simple link to the image will do now just fine. So it kind of would redirect you after an upload process to page where are links to all uploads just done.

I can only get it to work with 1st file and evidently other files won't even be uploaded then, hm.

 

Also would appreciate a code for blocking other filetypes but .jpeg, .jpg, .png, .gif and .bmp.

 

 

 

I'd highly appreciate help!

Link to comment
Share on other sites

You have an exit() in your while() loop. It is exiting the script after the first iteration!

 

instead of naming your upload files as 'file1', 'file2', 'file3', etc. Name them as an array. Then you can create a foreach loop to process each file.

<input type="file" name="files[]">

 

Also, do not use the FONT tag, it has been deprecated for MANY years.

 

I haven't tested this but it should get you in the right direction

<?php

define("MAX_COUNT", 10);
define("UPLOAD_DIRECTORY", "valokuvat/");
define("MAX_SIZE", 1000000);

if(!is_dir(UPLOAD_DIRECTORY))
{
    mkdir(UPLOAD_DIRECTORY, 0777);
}

if(isset($_FILES['files']))
{
    foreach($_FILES['files'] as $file) {

        if($file['size'] > MAX_SIZE)
        {
            echo "Liian iso tiedosto!<br>".MAX_SIZE." on raja"; break;
        }

        while(file_exists(UPLOAD_DIRECTORY.$file['name']))
        {
            $file['name']="$k".$file['name']; $k++;
        }

        move_uploaded_file($file['tmp_name'], UPLOAD_DIRECTORY.$file['name']);
    echo "xxx/waddap/valokuvat/".$file['name'];
    }
    exit();
}

$output = '';
for($i=1; $i<=MAX_COUNT; $i++)
{
    $fileInputs .= " <b><font style=\"color: #00FFFF;\">$i</font></b> <input type=\"file\" name=\"files[]\" size=\"75\"><br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Leettarin Uploader</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" href="xxx/twg/favicon.ico" />
</head>
<body background="Ready.jpg">
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" ENCTYPE="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="102400000">
        <?php echo $fileInputs; ?> 
        <input type="submit" value="Upload">
    </form>
</body>
</html>

Link to comment
Share on other sites

Thanks for the response!

Tried it out but unfortunately did not work.

 

Shortly put, regardless how many files I upload at once, be it one or five, it echoes this: "xxx.com/1, xxx.com/2, etc".

Also, now it refuses to save a single file on DB upon 'successful' upload process.

Link to comment
Share on other sites

I told you I didn't test it. I only said that you should use a name for the upload input fields so they will be treated as an array. I only posted the "sample" code to get you started. I leave it to YOU to debug the script.

 

Also, now it refuses to save a single file on DB upon 'successful' upload process.

There was nothing in your original code to upload anything to a database. you would need to modify that code accordingly too.

Link to comment
Share on other sites

OK, here is a working script. The array of file uploads was in a different format than I thought. I also fixed an error in the logic you had to rename the files to prevent duplicates. The original code would do the following:

 

- filename.txt

- 1filename.txt

- 21filename.txt

- 321filename.txt

 

I also moved the number to the end of the filename component: filename.txt, filename_1.txt, filename_2.txt, etc.

 

<?php

define("MAX_COUNT", 10);
define("UPLOAD_DIRECTORY", "valokuvat/");
define("MAX_SIZE", 1000000);

if(!is_dir(UPLOAD_DIRECTORY))
{
    mkdir(UPLOAD_DIRECTORY, 0777);
}

if(isset($_FILES['files']))
{
    foreach($_FILES['files']['name'] as $index => $filename)
    {
        //Check if upload field was empty
        if(empty($filename)) { continue; } //Go to next field

        //Check if upload exceeded max size
        if($_FILES['files']['size'][$index] > MAX_SIZE)
        {
            echo "Liian iso tiedosto!<br>".MAX_SIZE." on raja"; break;
        }

        //Rename if duplicate
        $k = 1;
        while(file_exists(UPLOAD_DIRECTORY.$filename))
        {
            $original = $_FILES['files']['name'][$index];
            $extpoint = strrpos($original, '.');
            $filename = substr($original, 0, $extpoint) . "_{$k}" . substr($original, $extpoint); $k++;
        }

        //Upload the file
        move_uploaded_file($_FILES['files']['tmp_name'][$index], UPLOAD_DIRECTORY.$filename);
        echo "xxx/waddap/valokuvat/$filename<br>\n";
    }
    exit();
}

$fileInputs = '';
for($i=1; $i<=MAX_COUNT; $i++)
{
    $fileInputs .= " <b><span style=\"color: #00FFFF;\">$i</span></b> <input type=\"file\" name=\"files[]\" size=\"75\"><br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Leettarin Uploader</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" href="xxx/twg/favicon.ico" />
</head>
<body background="Ready.jpg">
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" ENCTYPE="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="102400000">
        <?php echo $fileInputs; ?>
        <input type="submit" value="Upload">
    </form>
</body>
</html>

Link to comment
Share on other sites

I told you I didn't test it. I only said that you should use a name for the upload input fields so they will be treated as an array. I only posted the "sample" code to get you started. I leave it to YOU to debug the script.

Apologies. I meant no disrespect and you're right on that.

 

OK, here is a working script. The array of file uploads was in a different format than I thought. I also fixed an error in the logic you had to rename the files to prevent duplicates. The original code would do the following:

 

<snip>

 

I also moved the number to the end of the filename component: filename.txt, filename_1.txt, filename_2.txt, etc.

Thank you. It works like a charm! I'm still very new at php but willing to learn. However by googling I basically only found very advanced guides/snippets.

I've got decent experience with C++ and php reminds it in couple ways but yet it's completely own language. Have to work harder!

 

Anyway, I'm truly thankful for what you did  for me. I've been struggling with all this nearly whole night.

Also your post helped me to realize some basic things about php.

 

Will continue trying, thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.