Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
using php and mysql to provide a image library

Posted by kiko (kiko), 11 April 2007
hi, Graham.   i got a problem with using php and mysql to provide a image library. I have copy all the coding  Example - PHP form, Image upload. Store in MySQL database. Retreive,,and paste in my project...

the table pix can insert record for pid,imgdata and title.
but when the pic_up.php page have run, it appear many notices  like Notice: Use of undefined constant completed - assumed 'completed' in c:\program files\easyphp1-8\www\phprojek\pic_up.php on line 24

Notice: Undefined index: completed in c:\program files\easyphp1-8\www\phprojek\pic_up.php on line 24

Notice: Use of undefined constant title - assumed 'title' in c:\program files\easyphp1-8\www\phprojek\pic_up.php on line 50

Notice: Use of undefined constant imgdata - assumed 'imgdata' in c:\program files\easyphp1-8\www\phprojek\pic_up.php on line 51

Notice: Use of undefined constant gim - assumed 'gim' in c:\program files\easyphp1-8\www\phprojek\pic_up.php on line 62

Notice: Undefined index: gim in c:\program files\easyphp1-8\www\phprojek\pic_up.php on line 62

although the table can insert record,but the image cant appear at pic_up.php,, but appear at another page like latest.jmg.

belowing is my coding for pic_up.php.
<?php

// Connect to database

$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
       $errmsg = "Cannot connect to database";
       }
@mysql_select_db("db1");

// First run ONLY - need to create table by uncommenting this
// Or with silent @ we can let it fail every sunsequent time

$q = <<<CREATE
create table pix (
   pid int primary key not null auto_increment,
   title text,
   imgdata longblob)
CREATE;
@mysql_query($q);

// 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 pix (title, imgdata) values (\"".
               $_REQUEST[whatsit].
               "\", \"".
               $image.
               "\")");
       } else {
               $errmsg = "Too large!";
       }
}

// Find out about latest image

$gotten = @mysql_query("select * from pix order by pid 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("../wellimg/ctco.jpg","rb");
       $bytes = fread($instr,filesize("../wellimg/ctco.jpg"));
}

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

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

<html><head>
<title>Upload an image to a database</title>
<body bgcolor=white><h2>Here's the latest picture</h2>
<font color=red><?= $errmsg ?></font>
<center><img src= width=144><br>
<b><?= $title ?></center>
<hr>
<h2>Please upload a new picture and title</h2>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value=150000>
<input type="hidden" name="completed" value=1>
Please choose an image to upload: <input type="file" name="imagefile"><br>
Please enter the title of that picture: <input name="whatsit"><br>
then: <input type="submit"></form><br>

</body>
</html>

so,what's wrong with my coding? i need your help,thanks for reply.




Posted by admin (Graham Ellis), 13 April 2007
You can cure the warning messages either by chaning your error level or by surrounding the array element names with quotes - but they are just warnings and so won't effect, I don't think, the code operation.

If the image isn't appearing, I wonder if you have short tags on or off in your PHP configuration.   When you "view source" do you see the <?= string, or the name of the image?

Posted by kiko (kiko), 13 April 2007
thanks graham,

about the <?=string,or the name of the image>,when i view source, i just see<img src=? gim=1 width=144
but <?title?> have change to image name is correct.

may i ask about table pix's field name like  imgdata's record is like [BLOB-4.8kb] like this only is it?

is the [BLOB-4.8kb] instead the picture already?

is it the picture we upload will appear in www/phpprojek/  in img.file?

thanks for reply.

kiko


Posted by kiko (kiko), 13 April 2007
hi,graham,
about the notice warning i settle already,,just got 2 worning notice only is " undefined index :gim and undefined index completed,,what mistake in here ..
thaks for reply.

kiko.


Posted by admin (Graham Ellis), 19 April 2007
on 04/13/07 at 11:30:14, kiko wrote:
hi,graham,
about the notice warning i settle already,,just got 2 worning notice only is " undefined index :gim and undefined index completed,,what mistake in here ..
thaks for reply.

kiko.


As per previous answer, this is only a "notice" and you happen to have notices switched on - it's not really a mistake. To improve the code, you could add a call to one of PHP's functions to check if the array member exists before you access it.


Posted by admin (Graham Ellis), 19 April 2007
on 04/13/07 at 11:08:49, kiko wrote:
thanks graham,

about the <?=string,or the name of the image>,when i view source, i just see<img src=? gim=1 width=144
but <?title?> have change to image name is correct.

may i ask about table pix's field name like  imgdata's record is like [BLOB-4.8kb] like this only is it?

is the [BLOB-4.8kb] instead the picture already?

is it the picture we upload will appear in www/phpprojek/  in img.file?

thanks for reply.

kiko


I've read your questions several times but I'm not sure that I understand fully what you're asking. And I also notice that the code posted above is the code from our web sit example with very few changes - you've still got the names of specific files on our server in there, and of course that won't work without those files being present.

I would suggest you work though and complete the conversion of the script for use on your site, and while you're doing so, you read into the script and see what each section is doing - I think you'll find that easier that try to follow any explanations I might be able to give.    If you have problems with short sections after that, please do feel free to post up a follow up (10 to 20lines of source is an ideal length) and I'll have a further look.


Posted by ratty30 (ratty30), 7 November 2007
Hi,

I'm trying to use the picture upload too and get these errors on my page. I apologies if these are really simple mistakes as i'm new to website developing!

Warning: Unable to create 'latest.img': Is a directory in /www/client/www.wlamb.co.th/Social Compliance/pictures.php on line 18

Warning: Unable to move '/tmp/phpOLQ7ke' to 'latest.img' in /www/client/www.wlamb.co.th/Social Compliance/pictures.php on line 18

Thankyou Nick

Posted by admin (Graham Ellis), 7 November 2007
It looks like the directory into which you're attempting to upload the image on your server isn't writeable by the web server as an ordinary file, and you'll need to set this up manually.

The usual cause is that the directory does not exist, or doesn't have write permission set but your error message is slightly odd and I suspect you may already have a directory of the same name that you have coded for the plain file. Anyway - those are the things for you to look at / check.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho