[PHP]File Upload

Junior Spellweaver
Joined
Mar 4, 2009
Messages
165
Reaction score
5
Hello

I have coded a upload page in php but it is in 2 pages and i want it 2 be in one page il include my coding.
PHP:
<html>
<head>
<title>Test</title>
</head>

<body>
<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
PHP:
<?php
$target = "images/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

//This is our size condition
if ($uploaded_size > 1000000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file". basename( $_FILES['uploadedfile']['name']). " has been uploaded.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
So if anyone could code that into one page and also fix it so it actually shows the name of the file.

If so TY.
 
Check my image upload script. It's one page, but I didn't finish check file thing for 100%.

PHP:
<?php
$form = '<form action="upload-image.php" method="post" enctype="multipart/form-data">
        <p>
                Upload-file form:<br />
                <input type="file" name="myfile" /><br />
                <input type="submit" value="Upload file" />
        </p>
</form>
<code type="html">';
if (isset($_FILES['myfile']) AND $_FILES['myfile']['error'] == 0)
{
    if ($_FILES['myfile']['size'] <=  5242880)
    {
        $fileinfo = pathinfo($_FILES['myfile']['name']);
        $extension_upload = $fileinfo['extension'];
        $authorized_extensions = array('jpg', 'jpeg', 'gif', 'png');
        if (in_array($extension_upload, $authorized_extensions))
        {
            $name = str_replace('.', '', $_SERVER['REMOTE_ADDR']) . '_' . rand(100000000, 999999999) . '_' . basename($_FILES['myfile']['name']);
            move_uploaded_file($_FILES['myfile']['tmp_name'], 'uploads/' . $name);
            echo "Upload completed!";
        }
    }
}
else
{
echo $form;
}
?>
 
Check my image upload script. It's one page, but I didn't finish check file thing for 100%.

PHP:
<?php
$form = '<form action="upload-image.php" method="post" enctype="multipart/form-data">
        <p>
                Upload-file form:<br />
                <input type="file" name="myfile" /><br />
                <input type="submit" value="Upload file" />
        </p>
</form>
<code type="html">';
if (isset($_FILES['myfile']) AND $_FILES['myfile']['error'] == 0)
{
    if ($_FILES['myfile']['size'] <=  5242880)
    {
        $fileinfo = pathinfo($_FILES['myfile']['name']);
        $extension_upload = $fileinfo['extension'];
        $authorized_extensions = array('jpg', 'jpeg', 'gif', 'png');
        if (in_array($extension_upload, $authorized_extensions))
        {
            $name = str_replace('.', '', $_SERVER['REMOTE_ADDR']) . '_' . rand(100000000, 999999999) . '_' . basename($_FILES['myfile']['name']);
            move_uploaded_file($_FILES['myfile']['tmp_name'], 'uploads/' . $name);
            echo "Upload completed!";
        }
    }
}
else
{
echo $form;
}
?>

It uploads the file but it adds all these numbers in front of it.

So if anyone could give me something like this that uploads the file and can display the file name i would ty so much.
 
You just have to edit script a little bit. Its simple.
Numbers in front are ip and random number.

Here is script that you need, but notice that if you will upload file with same name then it will overwrite old file.

PHP:
   <?php
$form = '<form action="upload-image.php" method="post" enctype="multipart/form-data">
        <p>
                Upload-file form:<br />
                <input type="file" name="myfile" /><br />
                <input type="submit" value="Upload file" />
        </p>
</form>
<code type="html">';
if (isset($_FILES['myfile']) AND $_FILES['myfile']['error'] == 0)
{
    if ($_FILES['myfile']['size'] <=  5242880)
    {
        $fileinfo = pathinfo($_FILES['myfile']['name']);
        $extension_upload = $fileinfo['extension'];
        $authorized_extensions = array('jpg', 'jpeg', 'gif', 'png');
        if (in_array($extension_upload, $authorized_extensions))
        {
            move_uploaded_file($_FILES['myfile']['tmp_name'], 'uploads/' . basename($_FILES['myfile']['name']));
            echo "Upload completed!";
        }
    }
}
else
{
echo $form;
}
?>


---------- Post added at 03:52 PM ---------- Previous post was at 03:50 PM ----------

Offtop question: What are you making it for?
 
You just have to edit script a little bit. Its simple.
Numbers in front are ip and random number.

Here is script that you need, but notice that if you will upload file with same name then it will overwrite old file.

PHP:
   <?php
$form = '<form action="upload-image.php" method="post" enctype="multipart/form-data">
        <p>
                Upload-file form:<br />
                <input type="file" name="myfile" /><br />
                <input type="submit" value="Upload file" />
        </p>
</form>
<code type="html">';
if (isset($_FILES['myfile']) AND $_FILES['myfile']['error'] == 0)
{
    if ($_FILES['myfile']['size'] <=  5242880)
    {
        $fileinfo = pathinfo($_FILES['myfile']['name']);
        $extension_upload = $fileinfo['extension'];
        $authorized_extensions = array('jpg', 'jpeg', 'gif', 'png');
        if (in_array($extension_upload, $authorized_extensions))
        {
            move_uploaded_file($_FILES['myfile']['tmp_name'], 'uploads/' . basename($_FILES['myfile']['name']));
            echo "Upload completed!";
        }
    }
}
else
{
echo $form;
}
?>


---------- Post added at 03:52 PM ---------- Previous post was at 03:50 PM ----------

Offtop question: What are you making it for?

How do i make it display the name in "Upload completed of: (filename)
 
Hello

I have coded a upload page in php but it is in 2 pages and i want it 2 be in one page il include my coding.
PHP:
<html>
<head>
<title>Test</title>
</head>

<body>
<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
PHP:
<?php
$target = "images/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

//This is our size condition
if ($uploaded_size > 1000000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file". basename( $_FILES['uploadedfile']['name']). " has been uploaded.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
So if anyone could code that into one page and also fix it so it actually shows the name of the file.

If so TY.
Ofcourse it adds numbers, it's to stop files being over-written, what if someone uploads a file with the same name as a previous file, they will get mixed up.
 
How do i make it display the name in "Upload completed of: (filename)

PHP:
echo "Upload completed of: ". $_FILES['uploaded'][name];

Based on your original form.

Some notes about your first post:

If you add
PHP:
phpinfo(32);

To the page it posts to (in this case it posts to itself, but I don't really see that reflected in your code) then you will learn a lot about the file, by looking at the $_FILES array. That is where the $_FILES['uploaded'][name] and $_FILES['uploaded'][tmp_name] variables, among others are being stored. This is produced when your forms enctype is multipart/data, and the input field for the file name is type=file. (if you didn't know that)

I found your error handling a bit strange. (not wrong, just seems really redundant) I'd probably combine some if-else statements to say something like:

if (($ok == 0) && (code to copy files is success)){
//yay for you
}
else{
//generic error message
}

oh and back to the part where you said you wanted it to be in one page:
Change
<input type="submit" value="Upload" />
to
<input type="submit" value="Upload" name="Upload" />
wrap the file upload part in:
if (isset($_POST['Upload']){
// code to check file, and upload it
}

So that combined with what the others said should fix it for you. Although I didn't just do it for you, maybe would have taken less time than explaining, but then what would you learn? :P
 
Ofcourse it adds numbers, it's to stop files being over-written, what if someone uploads a file with the same name as a previous file, they will get mixed up.
No, the first will get overwritten.

This entire thread is retarded.

You could have just stacked the two pieces of code on top of one another, or included both pages into one page.. It's common sense.

"I have script a, and script b. How do I put them both on script c?" Stupid..
 
Back