I Have a problem about display an image and a descryption to a web page. as admin or some how i need to upload photos to my web site.(only to admin). so for that i need a help to do it (PHP CODE) please! im actually new to PHP.it would be very helpfull If someone can Help! Please

Recommended Answers

All 11 Replies

You'll have a hard time finding someone here to write the code for you. There are many tutorials out there that cover this.

Example:
http://blog.nerdstogeeks.com/2009/09/handling-images-using-php-mysql-in-3.html
http://techstream.org/Web-Development/PHP/Multiple-File-Upload-with-PHP-and-MySQL

Follow a tutorial online and get started somewhere. If you run into problems you can ask your questions along with your code here.

show your progress so far???
For admin only feature,you have to create login feature to your page.If the login member is admin then display a form to upload file something similar to http://www.tizag.com/phpT/fileupload.php .

Really I apprciate your help, indeed i wantd some good tutorials! thanks again. il refer and if some probs i'll let it to be here! :)

As others have said, you will need to do some of your own work, however I will give you a head start with the sort of things you will need to look for.

Essentially you want a secure file upload, and then on your site a reference to the image. To do this you will want a login page (as IIM suggested) so begin looking at $_SESSIONS. Once you have established who is trying to upload the file, either allow them to do so or not.
Just be careful here, you don't want anyone to upload something malicious!

Now you have your file uploaded, either rename them as a recurring number so you can loop through it if you want to display multiple images or store data in a database.
If you store the data in a database, you probably shall want to include the following:

  • File Location (or if this is to be fixed, only the name is required)
  • A description of the image (for the Alt tag and to display alongside)
  • The image name (for the Alt tag and to display alongside)

Don't bother storing the image as a BLOB in the database, this is insecure and in-efficient, just store the link in the database.

Personally I would separate this into two parts if I didn't know how to do any of it yet.

The first thing I would do is get my code working to handle my image uploads and inserting the upload information into my database. Then after that I would worry about the authentication part. Just because the two really are not connected and one can function without the other.

:) yes Of course. bt still my image is not dislayin? in my localhost.

post your code

<?php
$link = mysql_connect("localhost","root","") or die("Could not connect now");
mysql_select_db("prac") or die("Database not found");
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tmpname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");

$imgdir = "../blob/uploads/";
$imgname = $imgdir.$image_tmpname;
if(move_uploaded_file($_FILES['image_filename']['tmp_name'], $imgname))
{
list($width,$height,$type,$attr)= getimagesize($imgname);
switch($type)
{
 case 1:
  $ext = ".gif"; break;
 case 2:
  $ext = ".jpg"; break;
 case 3:
  $ext = ".png"; break;
 default:
   echo "Not acceptable format of image";
}
$insert = "insert into images (image_caption, image_username, image_date)
   values ('$image_caption','$image_username','$today')";
$insertresults = mysql_query($insert) or die(mysql_error());

$last_pic_id = mysql_insert_id();
$newfilename = $imgdir.$last_pic_id.$ext;
rename($imgname,$newfilename); 
}
?>

Here is your pic
<img src="uploads/<?php echo $last_pic_id.$ext; ?>" align='center'>

{

Have you created the folder:
$imgdir = "../blob/uploads/"; this will make this folder one level back from where your script is.

I would suggest for testing only, have the script in tyhe root of your site, and the folder on the above line renamed to uploads/ and create the folder from your root.

This is a very simple script to display files (check array for valid extensions) from a set folder:

$dir = 'photos/';

// Array of excepted data types
$file_display = array('jpg', 'jpeg', 'png', 'gif');    

if(file_exists($dir) == false){
    echo 'Directory \''.$dir.'\' not found!';
}  else {
    $dir_content = scandir($dir);

    foreach ($dir_content as $file){
        $file_type = strtolower(end(explode('.', $file)));

        if($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == TRUE){
            echo $file.'<br/><br/><img src="'.$dir.'/'.$file.'" alt="'.$file.'"/><br/>';

        }

    }
}

squidge was right. But I also prefer to have the database to store the file path for your Src in replace of what squidge's post on src="'.$dir.'/'.$file.'" this mannser it will be src="'.$filepath."' the$file path is what taken from sql database

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.