Jump to content

Archived

This topic is now archived and is closed to further replies.

Stevis2002

Help with PHP script please

Recommended Posts

Thanks for the help.

I had a permissions and directory error after that, but i sorted that one out, but then i get this error and i don't know what this means ...

The file has been uploaded
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\admin\includes\add_plant.php:45) in C:\Program Files\xampp\htdocs\admin\includes\add_plant.php on line 69

BTW....All data is going into db correctly now, although the image still isn't being uploaded/moved.

Share this post


Link to post
Share on other sites
The problem i now with

header(sprintf("Location: %s", $insertGoTo));

You can't call header once  data has already been output to the browser. In your case you have sent the "Your file has been uploaded message"

I'd remove the message. As you immediately want to go to another page the user wouldn't see it anyway.

Or, instead of header(), output a meta-refresh tag with a delay of a couple of seconds so user can read message before it goes to the nex page.

Share this post


Link to post
Share on other sites
Thanks again.....I have removed the message, but the page doesn't get forwarded on, and the image still isn't getting uploaded.

[code]
    mysql_select_db($database_localhost, $localhost);
    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
    $insertGoTo = "plants.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
    exit;
[/code]

Share this post


Link to post
Share on other sites
Are you getting an error message still?

The file isn't uploaded because that section of code is still using the non-existent 'image_name' index instead of 'name'

[quote]
    $uploaddir = '/plants/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['image_name']);

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {
        echo "The file ".  basename( $_FILES['userfile']['image_name']).
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
[/quote]

On reading that bit of code again, you need to change code so that in the event the upload fails you do not update the db.

Share this post


Link to post
Share on other sites
Thanks once again....I changed the image_name to name, but the code you mentioned is non existent now as i commented it out due to the header problem.

Share this post


Link to post
Share on other sites
As a minimum you need this to store the uploaded file

$uploaddir = '/plants/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir);

Share this post


Link to post
Share on other sites
Changed the layout so that if there is an error then it dies, but it still enters the info into the db, it doesn't mention any error, but the image still isn't in the correct directory :(

[code]
    $uploaddir = '\plants';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {


   
    $insertSQL = sprintf("INSERT INTO plants (image_name, image_type, image_size, path, plant_name, plant_type, planting_months, blooming_season, plant_description) VALUES ('%s', '%s', '%s', '%s', %s, %s, %s, %s, %s)",
    $fileImagename,
    $fileImagetype,
    $fileImagesize,
$uploaddir,
    GetSQLValueString($_POST['plant_name'], "text"),
    GetSQLValueString($_POST['plant_type'], "text"),
    GetSQLValueString($_POST['planting_months'], "text"),
    GetSQLValueString($_POST['blooming_season'], "text"),
    GetSQLValueString($_POST['plant_description'], "text"));

    mysql_select_db($database_localhost, $localhost);
    $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error());
    $insertGoTo = "plants.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
    exit;
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
[/code]

Share this post


Link to post
Share on other sites
    if(move_uploaded_file($_FILES['userfile']['tmp_name'], [color=red]$uploaddir[/color])) {

Oops - should be $uploadfile


If that doesn't work, try

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], realpath($uploadfile))) {

Share this post


Link to post
Share on other sites
BTW, now you've changed the value of $uploaddir, you will get something like

\plantsflower.jpg

in $uploadfile instead of

/plants/flower.jpg.

Better to use '/' instead of '\' even on Win systems.

Share this post


Link to post
Share on other sites
Barand....You my friend are a complete legend...a saint of all patient programmers!!!
With a little messing, i got rid of the error statement completely...don't need it, due to the fact that if there is a problem, the script wont go onto the forwarding page anyway, so i will now there is a prob, as the script is just to enter the info into the db anyway, and only i will be doing that!

I can manage the data layout and all that jazz myself for the users end.

So, Thanks once more for the patience and understanding you have with us [s]dunces[/s] newbies.

Steve

Share this post


Link to post
Share on other sites
Hi All,

Does anyone has an example of the code in the [b]/Connections/localhost.php [/b] file?

thanks in advance

bm

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

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.