/    Sign up×
Community /Pin to ProfileBookmark

Image / Text Upload Issue

I can’t seem to get the image upload and entry text (title, age, testimonial) to work the same time and on the same id.

When I connect to the database through

[CODE]include(“config.php”);[/CODE]

, it works for the entry text, but the image won’t show up. It will however add an id in the database for both entry text and image by adding 2 entries. One for the image which is not uploaded, and the other for the entry text which is uploaded. The oposite affect of this happens if I comment out the

[CODE]include(“config.php”);[/CODE]

, and connect to the database directly by using

[CODE]$errmsg = “”;
//if (! @mysql_connect(“localhost”,”root”,””)) {
// $errmsg = “Cannot connect to database”;
// }
@mysql_select_db(“album”);[/CODE]

I would REALLY appreciate any help offered.

[CODE]<?php
//error_reporting(0);
include(“config.php”);
// Connect to database

/*$errmsg = “”;
//if (! @mysql_connect(“localhost”,”root”,””)) {
// $errmsg = “Cannot connect to database”;
// }
@mysql_select_db(“album”);
*/

// Insert any new image into database

if ($_REQUEST[completed] == 1) {
// Need to add – check for large upload. Otherwise the code
// will just duplicate old file 😉
// ALSO – note that latest.img must be public write and in a
// live appliaction should be in another (safe!) directory.
move_uploaded_file($_FILES[‘imagefile’][‘tmp_name’],”latest.img”);
$instr = fopen(“latest.img”,”rb”);
$image = addslashes(fread($instr,filesize(“latest.img”)));
if (strlen($instr) < 149000) {
mysql_query (“insert into album (title, imgdata) values (“”.
$_REQUEST[whatsit].
“”, “”.
$image.
“”)”);
} else {
$errmsg = “Too large!”;
}
}

// Find out about latest image

$gotten = @mysql_query(“select * from album order by albumid desc limit 1”);
if ($row = @mysql_fetch_assoc($gotten)) {
$title = htmlspecialchars($row[title]);
$bytes = $row[imgdata];
} else {
$errmsg = “There is no image in the database yet”;
$title = “no database image available”;
// Put up a picture of our training centre
$instr = fopen(“../upload/example_1.jpg”,”rb”);
$bytes = fread($instr,filesize(“../upload/example_1.jpg”));
}

// If this is the image request, send out the image

if ($_REQUEST[gim] == 1) {
header(“Content-type: image/jpeg”);
print $bytes;
exit ();
}

if(isset($_POST[‘submit’]))

{//begin of if($submit).

// Set global variables to easier names

// and pervent sql injection and apostrophe to break the db.

$title = mysql_real_escape_string($_POST[‘title’]);

$age = mysql_real_escape_string($_POST[‘age’]);

$testimonial = mysql_real_escape_string($_POST[‘testimonial’]);

//check if (title) field is empty then print error message.

if(!$title){ //this means If the title is really empty.

echo “Error: Entry title is a required field. Please fill it.”;

exit(); //exit the script and don’t do anything else.

}// end of if

//run the query which adds the data gathered from the form into the database

$result = mysql_query(“INSERT INTO album (title, dtime, age, testimonial)

VALUES (‘$title’,NOW(),’$age’,’$testimonial’)”,$connect);

//print success message.

echo “<b>Thank you! News added Successfully!<br>You’ll be redirected to Home Page after (4) Seconds”;

echo “<meta http-equiv=Refresh content=4;url=index.php>”;

}//end of if($submit).

// If the form has not been submitted, display it!

else

{//begin of else

?>

<br>
<h3>::Add News</h3>
<font color=red>
<?= $errmsg ?>
</font>
<center>
<img src=?gim=1 width=500><br>
<b>
<?= $title ?>
</center>
<hr>
<h2>Please upload a new picture and title</h2>
<form action=”<?php $_SERVER[‘PHP_SELF’] ?>” method=”post” enctype=”multipart/form-data” name=”” id=””>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”15000000″>
Please choose an image to upload:
<input type=”file” name=”imagefile”>
<br>
<!–Please enter the title of that picture: <input name=”whatsit”><br>–>
then: <br>
<hr>
<input type=”hidden” name=”completed” value=”1″>
<p> Title:
<input name=”title” size=”40″ maxlength=”255″>
<br>
Age:
<textarea name=”age” rows=”7″ cols=”30″></textarea>
<br>
Testimonial:
<textarea name=”testimonial” rows=”7″ cols=”30″></textarea>
<br>
<input type=”submit” name=”submit” value=”Add News”>
</form>
<?php

}//end of else

?>
[/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@siavash1979Dec 09.2009 — Perhaps you're missing quotes in the line below and therefore your script never goes into that if statement:

if ($_REQUEST[completed] == 1)

it should be:

if ($_
REQUEST['completed'] == 1)

ALSO,

your line: move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img");

renames the image, but it keeps it in the upload_temo directory which is probably your /tmp/ folder. You should change "latest.img" to an absolute path like "/var/www/html/images/latest.img"
×

Success!

Help @Wayniac spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...