client upload & edit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emhmk1
Forum Newbie
Posts: 6
Joined: Sun Jun 15, 2008 4:34 am

client upload & edit

Post by emhmk1 »

Hi there,

I'm thinking of adding an upload facility to a clients website, basically, i need an upload facility where the client uploads an image. Then i need a textarea to write the description (the images will mainly be of animals and the textarea will be a description of the animals) ...when the client has written the description it is added to a text file then the both are incuded in the web page...

I know this must all sound very vague but basically i need direction of where to start on such a topic...

Thanks in advance
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: client upload & edit

Post by Randwulf »

If available, saving the comments and pic location to an SQL database would be ideal.

I assume you know how to accept file uploads with PHP. If not, check this out: http://www.tizag.com/phpT/fileupload.php

It will be sort of ugly without using a database, but you can do it.

First you should have your HTML file setup with a way of integrating pictures. For example, put in <!-- <img src=''> !><!--efgwrgrg !>. Make sure its commented out.

Here's the PHP part. You get the file, let's say you save it into /img/filenamehere.jpg (remember to match up the filetype correctly, you can do that with elements of $_FILE). Now you need to create a handle on the html file and copy it's contents into a string, for example:

$handle = fopen("index.html",'w');
$file_contents = file_get_contents(index.html);

Now add the picture. If you only have one slot for a picture on the site, just replace it:

$updated_content = str_replace("<!-- <img src=''> !>","<img src='/img/filenamehere.jpg'>",$file_contents);
fwrite($handle,$updated_content);

If you have several slots, you need to just replace the first one, so do:

$picpos = strpos(<!--efgwrgrg !>);
$firstpart = substr(0,$picpos);
$secondpart = substr($picpos,strlength($file_contents) - strlength($firstpart));
$firstpart = str_replace("<!-- <img src=''> !>","<img src='/img/filenamehere.jpg'>",$firstpart);
fwrite($handle,$firstpart . $secondpart);

And you can do something very similar for the text file.

But again, I recommend a DB.
emhmk1
Forum Newbie
Posts: 6
Joined: Sun Jun 15, 2008 4:34 am

Re: client upload & edit

Post by emhmk1 »

Thanks for the quick reply Randwulf,

What would your idea be on the database side? i'm pretty new to this but i have basic understanding of mysql

Cheers
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: client upload & edit

Post by Randwulf »

emhmk1 wrote:Thanks for the quick reply Randwulf,

What would your idea be on the database side? i'm pretty new to this but i have basic understanding of mysql

Cheers
You could have a field in the database that includes filenames, so every time they upload a picture it could be saved into the database with a comma on the end (to seperate the filenames). Then you could have PHP query for the filelist and explode() it into an array using the commas as the explode() parameter. Then you could just do a foreach:

foreach ($array as $file) echo "<img src='/img/$file'>";

Now that I think of it, you could store the filenames in a textfile too, which would be a better way to do it than the big long-winded one I gave you earlier :oops:
Post Reply