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 is
NULL

Thanks for any help!

Recommended Answers

All 5 Replies

Member Avatar for LastMitch

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:

Did it work for 1 variable POST? I mean the original code? If you wrote this code did it work for 1 upload?

I am can curious if it work for 1 variable then you did something to the code that it won't work for the second variable?

What changes did you do?

is this code some sort of a gaming logic? or a project based? I assume you need to give us information about the output you want to this matter. because why are you duplicating the file upload to become having 2 variable in post method?

What I meant is it takes file(s) and POST variable from a checkbox and then uploads the file to a directory. All of the random genneration is just to avoid naming problems

Member Avatar for LastMitch

All of the random genneration is just to avoid naming problems

The way you code it, it looks like you want to double check (verify) before upload the file?

Try to used this:

http://www.prosoftstudio.com/2011/02/php-tutorial-basic-usage-of-recaptcha-with-php/

Then it will look more reasonable with the upload file. The goal for your code is to make sure it is uploaded correctly. If you want to verify the file before it is upload then using a recaptcha, it won't intefere with your upload.

Right now, it's not working and it's not verify the file.

If you decide it to keep your current code, my suggestion is to used preg_match or preg_match_all and you can try this preg_match_all tutorial:

http://www.web-development-blog.com/archives/parse-html-with-preg_match_all/

Right now your strings are not reading correctly. So you need a preg match() function.

What this script does is upload a file or files and then generate a random name so if the file image.png is uploaded 10 times there won't be duplication issues. It then sends the owner of the file and other info to the database.
Anyway, I've fixed it now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.