Page 1 of 1

how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 2:51 am
by lukelee
Hey, guys, i am new to PHP, currently i am learning how to make a basic cms, but have difficulty on how to change the image which is on a webpage. do you save the image in database? and when i want to change it, i just login to admin page, change the image in databse? or i upload the image, then select the image? i dont know. can anyone give me some samples? maybe some codes. thanks a lot

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 3:19 am
by Paul Arnold
Hi.

I tend to make a script to upload and resize the images to the right dimensions and save the filename in the database.
I then get the page to point to the right folder and the filename from the DB.

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 3:23 am
by deejay
normally you'd put your images on your server and store a record of the image in your database.

check php.net for these things

$_FILES

move_uploaded_file


that should get you started

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 4:26 am
by lukelee
i have tried this , i can upload the image to the directory in my host, but how do i use this image to replace to old one?
and also i have made an table for image, it has: image-id, image-name,image-data. what do i save in image-data? the image url?

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 6:03 am
by Paul Arnold
That all depends how you've got your page set up mate.

Say where you have the image in the page you'll want something like...

Code: Select all

 
<?PHP
$queryText = "SELECT image-name FROM image WHERE image-id = '[color=#BF0000]whatever the image id is[/color]'";
$query = mysql_query($queryText, $yourdatabaseconnection);
 ?>
<img src="images/<?PHP echo $row['image-data']; ?>" />
 
From the looks of things if your page content is generated by the CMS you would be storing an image id for the image in your page content table which is linking to your image table.

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 6:57 am
by lukelee
Paul Arnold wrote:That all depends how you've got your page set up mate.

Say where you have the image in the page you'll want something like...

Code: Select all

 
<?PHP
$queryText = "SELECT image-name FROM image WHERE image-id = '[color=#BF0000]whatever the image id is[/color]'";
$query = mysql_query($queryText, $yourdatabaseconnection);
 ?>
<img src="images/<?PHP echo $row['image-data']; ?>" />
 
From the looks of things if your page content is generated by the CMS you would be storing an image id for the image in your page content table which is linking to your image table.
thanks, but what is $row['image-data']; ? if my image url is http://www.luke.com.au/test/images/book.jpg, what do i write in image-data? i tried many ways, but just the image not showing on the webpage.

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 8:37 am
by Paul Arnold
Sorry, my bad.

Code: Select all

 
<?PHP
$queryText = "SELECT image-data FROM image WHERE image-id = '[color=#BF0000]whatever the image id is[/color]'";
$query = mysql_query($queryText, $yourdatabaseconnection);
$row = mysql_fetch_assoc($query);
 
 ?>
<img src="test/images/<?PHP echo $row['image-data']; ?>" />
 
The field 'image-data' would contain 'book.jpg'.

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 9:41 am
by lukelee
Paul Arnold wrote:Sorry, my bad.

Code: Select all

 
<?PHP
$queryText = "SELECT image-data FROM image WHERE image-id = '[color=#BF0000]whatever the image id is[/color]'";
$query = mysql_query($queryText, $yourdatabaseconnection);
$row = mysql_fetch_assoc($query);
 
 ?>
<img src="test/images/<?PHP echo $row['image-data']; ?>" />
 
The field 'image-data' would contain 'book.jpg'.
oh, yes! man, i made it. the code is:
<?PHP
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE pid = '1'");
$row = mysql_fetch_array($query);
?>
<img src="uploaded_files/<?PHP echo $row['imagedata']; ?>" align="left"/>

thanks a lot.

now i have another question, i have already made an upload function which uploads images to image folder, but all images i uploaded have their name changed, i.e. image "home.jpg" after uploaded becomes "1221058042-home.jpg", what i want is to auto replace a image, i.e. i upload an image "home.jpg", after uploaded, home.jpg auto replace the image "book.jpg" , and the name also change to book.jpg.
any suggestion? or any better ways?

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 10:21 am
by Paul Arnold
Sorry, I don't quite understand what you mean.
Are you wanting to essentially overwrite the old image when you upload a new one?

Because all you'd do here is upload the new image, change the filename in the database to the new image and then remove the old image using the unlink() function.

Is that what you're looking to do?

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 10:48 am
by lukelee
Paul Arnold wrote:Sorry, I don't quite understand what you mean.
Are you wanting to essentially overwrite the old image when you upload a new one?

Because all you'd do here is upload the new image, change the filename in the database to the new image and then remove the old image using the unlink() function.

Is that what you're looking to do?
yeah, thats what i mean, like i want to upload a new image book2.jpg, and in database the image-data is book.jpg, once i uploaded book2.jpg, the image-data should change to book2.jpg. then the webpage's image will be changed. do you know how to make this? store the file name into database?

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 10:51 am
by Paul Arnold
Yeah, just update the filename.

Code: Select all

 
<?PHP
require_once('db.php');
$query = mysql_query("UPDATE image SET image-data = 'book2.jpg' WHERE pid = '1'");
?>
 

Re: how to make cms to let people change the image on a webpag

Posted: Wed Sep 10, 2008 11:22 am
by lukelee
Paul Arnold wrote:Yeah, just update the filename.

Code: Select all

 
<?PHP
require_once('db.php');
$query = mysql_query("UPDATE image SET image-data = 'book2.jpg' WHERE pid = '1'");
?>
 
sorry, i should make it clear, book2.jpg is a sample, it could be any name. people would upload any images with different names, what i mean is when people want to change the image on the webpage, they just upload a new image, and this image will replace the old one and display on screen.

Re: how to make cms to let people change the image on a webpag

Posted: Thu Sep 11, 2008 2:59 am
by Paul Arnold
Yeah, tht's fine mate. You just need to get the filename of the file you've uploaded and make that update query change the filename in the database.

The book2.jpg in that query was just following your example. You'd change that to whatever the new filename is.

This may just be from the $_FILES array or it may be something different depending on your upload script.

Re: how to make cms to let people change the image on a webpag

Posted: Sat Sep 13, 2008 2:32 am
by lukelee
Paul Arnold wrote:Yeah, tht's fine mate. You just need to get the filename of the file you've uploaded and make that update query change the filename in the database.

The book2.jpg in that query was just following your example. You'd change that to whatever the new filename is.

This may just be from the $_FILES array or it may be something different depending on your upload script.

Code: Select all

<?php  
 
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
 
$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploaded_files/';
 
$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.form.php';
 
$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.success.php';
 
$fieldname = 'file';
 
 
$errors = array(1 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');
 
isset($_POST['submit'])
    or error('the upload form is neaded', $uploadForm);
 
$active_keys = array();
foreach($_FILES[$fieldname]['name'] as $key => $filename)
{
    if(!empty($filename))
    {
        $active_keys[] = $key;
    }
}
 
count($active_keys)
    or error('No files were uploaded', $uploadForm);
        
foreach($active_keys as $key)
{
    ($_FILES[$fieldname]['error'][$key] == 0)
        or error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm);
}
    
foreach($active_keys as $key)
{
    @is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
        or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);
}
    
 
foreach($active_keys as $key)
{
    @getimagesize($_FILES[$fieldname]['tmp_name'][$key])
        or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);
}
    
 
foreach($active_keys as $key)
{
    $now = time();
    while(file_exists($uploadFilename[$key] = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'][$key]))
    {
        $now++;
    }
[color=#FF0000] require_once('db.php');
$query = mysql_query("UPDATE image SET imagedata = '$uploadFilename[$key]' WHERE pid = '1'");
[/color]
 
}
 
foreach($active_keys as $key)
{
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
        or error('receiving directory insuffiecient permission', $uploadForm);
}
 
 
[color=#FF0000]header('Location: ' .$uploadSuccess);[/color]
 
function error($error, $location, $seconds = 5)
{
    header("Refresh: $seconds; URL=\"$location\"");
    echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
    '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
    '<html lang="en">'."\n".
    '   <head>'."\n".
    '       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
    '       <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
    '   <title>Upload error</title>'."\n\n".
    '   </head>'."\n\n".
    '   <body>'."\n\n".
    '   <div id="Upload">'."\n\n".
    '       <h1>Upload failure</h1>'."\n\n".
    '       <p>An error has occured: '."\n\n".
    '       <span class="red">' . $error . '...</span>'."\n\n".
    '       The upload form is reloading</p>'."\n\n".
    '    </div>'."\n\n".
    '</html>';
    exit;
}
 
?>
hi, here is the codes. here are 2 problems, 1 is what ivs got in imagedata is something like: /var/virtual/web/w0333/subdomain/luke//test3/uploaded_files/book2.jpg i just want a single book2.jpg. another problem is after the sql codes, the page wont turn to success page, the page will become blank. thanks for the help.

Re: how to make cms to let people change the image on a webpag

Posted: Mon Sep 15, 2008 6:35 am
by lukelee
can anyone help me on this?