Jump to content

Recommended Posts

I have an image upload script that works fine, but i decided to make another one for a dif section of the site. This time though i needed to include things like name and description aswell as the image to upload. So i copied the upload.php file and added a few more things in but when i click uppload it goes straight to a blank screen and does nothing. Is there something wrong wih my code?

 

Form

echo '<form enctype="multipart/form-data" action="uploader2.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60"><font color=#ff0000 size=1> * required</font>
</td></tr>

<tr><td>Render Name:</td><td>
<input type="text" name="rendername" maxlength="50"><font color=#ff0000 size=1> * required</font>
</td></tr>

<tr><td>Description:</td><td>
<input type="text" name="description" maxlength="50"><font color=#ff0000 size=1> * required</font>
</td></tr>

<br><br>

<tr><td>Choose an Image:</td><td>
<input name="userfile" type="file" id="userfile" /><br />
<input type="submit" value="Upload Render" name="upload2" id="upload2" />
</td></tr></table></form></p>';


echo '</div>';

 

upload2.php

<?php

ob_start();
include('header.php');
include('db.php');
    $db=mysql_connect($db_host,$db_user,$db_pass)
or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db($db_name,$db);

$uploadDir = 'uploads/';

if(isset($_POST['upload2']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$username = $_POST['username'];
$render = $_POST['rendername'];
$description = $_POST['description'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$filePath = addslashes($filePath);
} 

$sql = ("UPDATE publicgallery SET path='$filePath', username='$username', 

name='$rendername', description='$description', link='$filePath'") or die

(mysql_error());
$result = mysql_query($sql, $db);


echo '<br><br>

<div id="page">
	<div id="content">
	  <div class="post">
<p class="meta">Your image has been successfully uploaded  <img 

src="images/img08.png" alt="bullet"></p>
			<div class="entry">';
echo 'Your new image will now be visible on the Public Gallery. Click <a 

href=public_gallery.php>here</a> to go back.
</div></div></div>';
include('footer.php');
}

ob_flush(); 
?>

Link to comment
https://forums.phpfreaks.com/topic/182754-image-and-text-upload/
Share on other sites

cheers for the reply, heres what i came out with:

 

POST:Array
(
    [MAX_FILE_SIZE] => 200000
    [username] => doddsey_65
    [rendername] => cell
    [upload] => Upload Render
)
FILES:Array
(
    [userfile] => Array
        (
            [name] => cell.jpg
            [type] => image/pjpeg
            [tmp_name] => /tmp/phpseGNuN
            [error] => 0
            [size] => 4499
        )

)

 

All looks ok to me here, can you see anything wrong?

The form code you posted above is not the form you are actually using.

 

The form code you posted is using upload2 as the name of your submit button and that is the name that your form processing code is testing for.

 

The form you are actually using has a submit button named upload, so your form processing code is skipping over the if(isset($_POST['upload2'])){....} conditional code.

okay im confused lol. The form i am using has a submit button like so:

 

<input type="submit" value="Upload Render" name="upload2" id="upload2" />

 

and the code in upload2 for it is

 

if(isset($_POST['upload2']))

 

which i thought was right

This is from the printout you just posted -

    [upload] => Upload Render

 

That clearly shows that the form is setting a submit button named upload

 

Have you refreshed your form page in your browser after making any changes to it so that those changes would be reflected in the HTML?

yeh i always refresh the page after editing files, but i understand what you mean now. is there anything else that could cause this?

could it be due to the form type(multipart/form-data) or because there is another form(on a dif page) with the same names(eg userfile)

Browsers don't make up HTML code. The form you are browsing to that has an action attribute of  action="uploader2.php" also has a submit button with a name attribute of name="upload"

 

You would need resolve your different forms, actions, submit button names, and the names of the variables you use in the form processing code.

 

Your description field in the form code you posted did not show up in the output from the print_r() instruction, so clearly (again) the form you are browsing to is not the form code you posted in this thread. The form code you did post in this thread produces the following output from the print_r() instructions (for all empty form fields) -

 

POST:Array
(
    [MAX_FILE_SIZE] => 200000
    [username] => 
    [rendername] => 
    [description] => 
    [upload2] => Upload Render
)
FILES:Array
(
    [userfile] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

 

so basically change all of the variables in upload2.php so they are dif from the other upload form(upload.php) and the same in the forms. When i got rid of the upload.php and sed this one(upload2.php) it did work but i will try them both. Oh and when i say it did work it wasnt uploading or inserting the image details. I echoed the sql and it didnt display the filepath, so i echoed the $filepath and came up with nothing. Since the first upload script is gone i know this is nothing to do with it. When i set about doing this thing i thought it would be easy lol, just insert a few things in the db and recall them. How wrong was I. Anyway have ou any idea why $filepath is returning nothing? It worked fine in the first upload.php.

 

<?php

ob_start();
include('header.php');
include('db.php');
    $db=mysql_connect($db_host,$db_user,$db_pass)
or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db($db_name,$db);

$uploadDir = 'public/';

if(isset($_POST['upload2']))
{
$fileName = $_FILES['userfile2']['name'];
$tmpName = $_FILES['userfile2']['tmp_name'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$filePath = addslashes($filePath);
} 

$sql="INSERT INTO publicgallery (path, name, username, description)
VALUES
('$filepath','$_POST[rendername]','$_POST[username]','$_POST[description]')";
$result = mysql_query($sql, $db);

echo 'FILEPATH:(' .$filepath. ')';
echo $sql;
echo '<br><br>

<div id="page">
	<div id="content">
	  <div class="post">
<p class="meta">Your image has been successfully uploaded  <img src="images/img08.png" alt="bullet"></p>
			<div class="entry">';
echo 'Your new image will now be visible on the Public Gallery. Click <a href=public_gallery.php>here</a> to go back.
</div></div></div>';
include('footer.php');
}

ob_flush(); 
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.