Jump to content

If file above 5.5MB, redirect to not_uploaded.php - it doesnt though!


AJ_V

Recommended Posts

Hi everyone! New to the forum due to the fact that I have just started learning PHP! I have currently done an upload script that I find works quite well, the script is written in PHP and will upload the file to the folder /upload/, make sure the file is under 5.5MB, add a random four digit number to all files uploaded to make them unique and change all files uploaded with the .php extension into .phps to prevent malicious execution. I would really like to implement an upload status bar on my website (http://www.vee-media.com), showing maybe the percentage of how much of the file has uploaded etc? Here is my script so far:

[code]<?php
session_start();
$target = basename( $_FILES['uploaded']['name']) ;
$ok=100;
$target = str_replace(php,phps,$target);
$random_digit=rand(0000,9999);
$target = $random_digit.$target;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
if ($uploaded_size > 5500000)
{
echo "Your file is too large.<br>";
$ok=0;
}
$_SESSION['filename'] = "http://vee-media.com/upload/$target";
$URL="http://www.vee-media.com/uploaded.php";
header ("Location: $URL");
}
else
{
$URL="http://www.vee-media.com/not_uploaded.html";
header ("Location: $URL");
}
?>[/code]

If you have experience with Windows Update, you have probably seen the scrolling green status bar - I am looking for something like that but with like a percentage or the amount of MB/KB already uploaded?

Really grateful for any help!

Regards,
AJ.

P.S Hope you all had a merry christmas and have a smashing new year!
Hi all! Got a bit of a problem with my upload script - it is all working fine except the file size limitation. When the user tries to upload something above 5.5MB, it should redirect to not_uploaded.php but instead it stays on the upload php file (php.php) and displays the following error:

[b]Your file is too large.

Warning: Cannot modify header information - headers already sent by (output started at /hsphere/local/home/cvenning/vee-media.com/upload/php.php:12) in /hsphere/local/home/******/vee-media.com/upload/php.php on line 17[/b]

Here is the PHP script:

[code]<?php
session_start();
$target = basename( $_FILES['uploaded']['name']) ;
$ok=100;
$target = str_replace(php,phps,$target);
$random_digit=rand(0000,9999);
$target = $random_digit.$target;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
if ($uploaded_size > 5500000)
{
echo "Your file is too large.<br>";
$ok=0;
}
$_SESSION['filename'] = "http://vee-media.com/upload/$target";
$URL="http://www.vee-media.com/uploaded.php";
header ("Location: $URL");
}
else
{
$URL="http://www.vee-media.com/not_uploaded.php";
header ("Location: $URL");
}
?> [/code]

Any help is gratefully appreciated!

Regards,
AJ.
[code]
<?php
ob_start();
session_start();
$target = basename( $_FILES['uploaded']['name']) ;
$ok=100;
$target = str_replace(php,phps,$target);
$random_digit=rand(0000,9999);
$target = $random_digit.$target;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
if ($uploaded_size > 5500000)
{
echo "Your file is too large.<br>";
$ok=0;
}
$_SESSION['filename'] = "http://vee-media.com/upload/$target";
$URL="http://www.vee-media.com/uploaded.php";
header ("Location: $URL");
}
else
{
$URL="http://www.vee-media.com/not_uploaded.php";
header ("Location: $URL");
}
ob_end_flush();
?>
[/code]
Try:
[code]<?php
session_start();

function redirect($URL)
{
    header ("Location: $URL");
}

$target = basename($_FILES['uploaded']['name']) ;

$ok = 100;

$target = str_replace('php', 'phps', $target);

$random_digit = rand(0000,9999);

$target = $random_digit.$target;

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
    if ($uploaded_size > 5500000)
    {
        echo "Your file is too large.<br>";
        $ok = 0;
    }
    else
    {
        $_SESSION['filename'] = 'http://vee-media.com/upload/' . $target;

        $ok = 100;
    }
}
else
{
    $ok = 0;
}

if($ok != 100)
    redirect('http://www.vee-media.com/not_uploaded.php');
else
    redirect('http://www.vee-media.com/uploaded.php');
}

?>[/code]
Thank you very much wild teen88! It is working perfectly now, I had to take out the final "}" because it gave a parse error but all is well now! Here is the final script:

[code]<?php
session_start();

function redirect($URL)
{
    header ("Location: $URL");
}

$target = basename($_FILES['uploaded']['name']) ;

$ok = 100;

$target = str_replace('php', 'phps', $target);

$random_digit = rand(0000,9999);

$target = $random_digit.$target;

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
    if ($uploaded_size > 5500000)
    {
        echo "Your file is too large.<br>";
        $ok = 0;
    }
    else
    {
        $_SESSION['filename'] = 'http://vee-media.com/upload/' . $target;

        $ok = 100;
    }
}
else
{
    $ok = 0;
}

if($ok != 100)
    redirect('http://www.vee-media.com/not_uploaded.php');
else
    redirect('http://www.vee-media.com/uploaded.php');
?>[/code]

Now, anyone know how I could integrate a upload status bar to show what percentage has uploaded?!

Regards,
AJ

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.