OK I'm pretty green at this so do bear with me. What I want to do is have an area where people can upload files to me. I found a script that I'm trying to make work and I've almost got it there but I'm having an issue. Everything actually functions correctly, the files are being uploaded and I'm receiving them. The problem is when the user chooses the file and hits "Upload" they are taken to a plain white page and either says the "Upload Successful" or "File could not be uploaded" with some statistics. What I'd rather have is two custom pages that tie in with my site theme. So if the upload is successful they're taken to "success.html" and if it fails they're take to "upload_error.html". Here's the upload script...

<?php

$SafeFile = $HTTP_POST_FILES['ufile']['name'];
$SafeFile = str_replace("#", "No.", $SafeFile);
$SafeFile = str_replace("$", "Dollar", $SafeFile);
$SafeFile = str_replace("%", "Percent", $SafeFile);
$SafeFile = str_replace("^", "", $SafeFile);
$SafeFile = str_replace("&", "and", $SafeFile);
$SafeFile = str_replace("*", "", $SafeFile);
$SafeFile = str_replace("?", "", $SafeFile);

$uploaddir = "uploads/";
$path = $uploaddir.$SafeFile;

if($ufile != none){ //AS LONG AS A FILE WAS SELECTED...

    if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED...

        //GET FILE NAME
        $theFileName = $HTTP_POST_FILES['ufile']['name'];

        //GET FILE SIZE
        $theFileSize = $HTTP_POST_FILES['ufile']['size'];

        if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
            $theDiv = $theFileSize / 1000000;
            $theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
        } else { //OTHERWISE DISPLAY AS KB
            $theDiv = $theFileSize / 1000;
            $theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
        }

echo <<<UPLS
<table cellpadding="5" width="300">
<tr>
    <td align="Center" colspan="2"><font color="#009900"><b>Upload Successful</b></font></td>
</tr>
<tr>
    <td align="right"><b>File Name: </b></td>
    <td align="left">$theFileName</td>
</tr>
<tr>
    <td align="right"><b>File Size: </b></td>
    <td align="left">$theFileSize</td>
</tr>
<tr>
    <td align="right"><b>Directory: </b></td>
    <td align="left">$uploaddir</td>
</tr>
</table>

UPLS;

    } else {

//PRINT AN ERROR IF THE FILE COULD NOT BE COPIED
echo <<<UPLF
<table cellpadding="5" width="80%">
<tr>
<td align="Center" colspan="2"><font color=\"#C80000\"><b>File could not be uploaded</b></font></td>
</tr>

</table>

UPLF;
    }
}

?>

Any feedback would be greatly appreciated. Thanks!

Member Avatar for diafol

WHich version of php are you using?? I thought $HTTP_POST_FILES went out with the Ark.

I'm open to any suggestions. Am I using some really antiquated format? How should I recode to get my desired result?

Member Avatar for diafol

$_FILES since php4.1 or around there I think.

OK

if (...){ 
  ...
  header("Location: success.html");
}else{
  ...
  header("Location: fail.html");
}

Do that instead of your heredoc stuff.

ardav thank you for your answer.. however you're giving me too much credit HahHAha! I still don't know what I can delete and what needs to stay. I'm presuming the places where you have "..." there's actually supposed to be info in there and not just dots. At the risk of sounding pathetic and desperate can you or anyone reading this spell out exactly what the code should be. I've been trying to apply logic and figure it out here for 45min, I'm usually not that "gimme the answer" kinda guy, I want to understand the process. However in this case I just don't speak enough of the language :(

Member Avatar for diafol

Try this:

$SafeFile = $_FILES['ufile']['name'];
$SafeFile = str_replace("#", "No.", $SafeFile);
$SafeFile = str_replace("$", "Dollar", $SafeFile);
$SafeFile = str_replace("%", "Percent", $SafeFile);
$SafeFile = str_replace("^", "", $SafeFile);
$SafeFile = str_replace("&", "and", $SafeFile);
$SafeFile = str_replace("*", "", $SafeFile);
$SafeFile = str_replace("?", "", $SafeFile);
 
$uploaddir = "uploads/";
$path = $uploaddir.$SafeFile;
 
if($ufile != none){ //AS LONG AS A FILE WAS SELECTED...
 
    if(copy($_FILES['ufile']['tmp_name'], $path)){ //IF IT HAS BEEN COPIED...
 
        //GET FILE NAME
        $theFileName = $_FILES['ufile']['name'];
 
        //GET FILE SIZE
        $theFileSize = $_FILES['ufile']['size'];
 
        if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
            $theDiv = $theFileSize / 1000000;
            $theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
        } else { //OTHERWISE DISPLAY AS KB
            $theDiv = $theFileSize / 1000;
            $theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
        }
        //a dirty, lazy way of doing it:
        header("Location: success.php?n=$theFileName&s=$theFileSize&u=$uploaddir");
    } else {
        header("Location: fail.php");
    }
}
?>

I didn't study the code too closely, so apols if it doesn't quite work.

In the success.php file:

do your html, head, body bits etc... then:

if(isset($_GET['n']) && isset($_GET['s']) && isset($_GET['u'])){
  $theFileName = $_GET['n'];
  $theFileSize = $_GET['s'];
  $uploaddir = $_GET['u'];

echo <<<UPLS
<table cellpadding="5" width="300">
<tr>
    <td align="Center" colspan="2"><font color="#009900"><b>Upload Successful</b></font></td>
</tr>
<tr>
    <td align="right"><b>File Name: </b></td>
    <td align="left">$theFileName</td>
</tr>
<tr>
    <td align="right"><b>File Size: </b></td>
    <td align="left">$theFileSize</td>
</tr>
<tr>
    <td align="right"><b>Directory: </b></td>
    <td align="left">$uploaddir</td>
</tr>
</table>
UPLS;

Awesome! Thank you for taking the time to help spoon feed me the answer. All I had to do is change...

header("Location: success.php?n=$theFileName&s=$theFileSize&u=$uploaddir");
    } else {
        header("Location: fail.php");

to

header("Location: success.html");
    } else {
        header("Location: fail.html");

Didn't even need the second script you posted, just the first one. Not sure if you didn't understand what I was trying to do, or if I don't understand the purpose of the second script. Either way thank you.. thank you.. thank you!

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.