Question..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Question..

Post by nick2 »

Am I allowed to ask another question or am I going to get accused?

I've been trying all yesterday to get my code to work.. but it just makes no errors and simply just doesn't upload anything..

Code: Select all

<?php

$path = "";
$max_size = 200000;

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "upload failed!<br>\n"; exit; } else { echo "upload sucessful<br>\n"; }

echo "File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
} else { 
echo "Wrong file type<br>\n"; exit; }

}

?>
I've been trying different things.. looking at the php manual and it all looks right yet nothing works.. if one of you would be kind enough to maybe explain whats going on? :P

could it be a setting server side that blocks it?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Quick question, just so I know before I try to help - which version of PHP are you using?

Mac
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

PHP Version 4.3.2
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Few questions for you:
  • What does the form look like through which you try to upload the file?
  • What do you get if you put:

    Code: Select all

    echo '<pre>';
    print_r($_FILES);
    echo '</pre>';
    at the top of the form processing page?
  • What is the output of:

    Code: Select all

    echo '<pre>';
    echo 'PHP Version: '.phpversion()."\n";
    echo 'Display Errors: '.ini_get('display_errors')."\n";
    echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n";
    echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n";
    echo '</pre>';
Mac
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

Upload process:

Code: Select all

Array
(
)

Output of the other stuff:

Code: Select all

PHP Version: 4.3.2
Display Errors: 1
Error Level: Not E_ALL
Register Globals: On
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

And the initial form?

Mac
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

Oh sorry forgot:

Code: Select all

<center><form method="POST" action="upload.php">
  <p><input type="file" name="userfile" size="20">
<input type="submit" value="Do It!" name="B1">
  - <font size="2">Upload an image</font></p>
</form></center>

also since your here..is there a simple way to pop up a window without using javascript.. maybe using php?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Before I have a look at the form - popups are a clientside thingie, therefore you need JavaScript.

Mac
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

ok.. :(

javascript just seems very confusing so I tend to avoid it. :oops:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try changing:

Code: Select all

<form method="POST" action="upload.php">
to

Code: Select all

<form enctype="multipart/form-data" action="upload.php" method="POST">
and definitely have a read of:
http://php.net/manual/en/features.file-upload.php
Mac
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

Thanks its worked ^__^

but.

^-- gotta hate the buts..

Code: Select all

<?php
include 'includes.php';
###
$path = $_COOKIE[user] . "/";
$max_size = 200000;

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>\n"; exit; }

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "upload failed!<br>\n"; exit; } else { 
header("location: Http://www.slices.net/pictures.php");
}
?>

KEEPS on giving me parse errors I keep changing everything around and it should work by looks of it yet it doesn't it gives me errors on the header around there...:(
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

post the error messages.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

must be a PHP Parse error: parse error, unexpected $end in blablabla
If you take more care about indenting your code you will find that there are closing curly brackets missing.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

A tip. Try to use { } and indents (preferably 4 spaces) like i rewrote it.
Its easier to spot issues with missing brackets.

(Note: Not entirely sure this works, but it looks okay from m position)

Code: Select all

<?php
    include 'includes.php';
    ###
    $path = $_COOKIE[user] . "/";
    $max_size = 200000;

    if (!isset($HTTP_POST_FILES['userfile'])) { 
        exit;
    }

    if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

        if ($HTTP_POST_FILES['userfile']['size']>$max_size) { 
            echo "The file is too big<br>\n"; 
            exit; 
        }

        if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {

            if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { 
                echo "The file already exists<br>\n"; 
                exit; 
            }

            $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path . $HTTP_POST_FILES['userfile']['name']);

            if (!$res) { 
                echo "upload failed!<br>\n"; 
                exit; 
            } else {
                header("location: Http://www.slices.net/pictures.php");
            }

        } // missing

    } // missing
?>
Post Reply