Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with an article submission php script. 1

Status
Not open for further replies.

RZillmer

Programmer
Nov 20, 2001
42
US
I'm sorry if these questions are pretty simple or have been answered before. I just started teaching myself PHP a week ago.

I'm trying to set up a page for users to input humor/news articles. Here's a very basic run-down of the psuedo-code:

If the user has submitted an article
{
upload article and images into database
}
else
{
If the user is previewing an article
{
Display article with POST variables
}
Display article submission form with values previously entered (if there are any)
Preview button makes the page call itself
Submit button makes the page call itself
}

That's pretty much all there is to it. I have the following problems:
1) some of the input areas on my form are of type TEXTAREA. They are not retaining the information put into them after you hit preview, although the information is showing up in the article preview. Here is a code sample of what I am trying to do:

<TEXTAREA name=&quot;paragraph1&quot; rows=&quot;10&quot; cols=&quot;50&quot; value=&quot;<?php echo $paragraph1?>&quot;></TEXTAREA><BR><BR>

2) some of the input areas on my form are of type file (for the locations of images to upload). These are not retaining their values either. Here's a code sample of that:

<INPUT name=&quot;pic1&quot; type=&quot;file&quot; size=&quot;37&quot; value=&quot;<?php echo $pic1?>&quot;><BR>

I also tried this with no luck:

<INPUT name=&quot;pic1&quot; type=&quot;file&quot; size=&quot;37&quot; value=&quot;<?php echo $pic1_name?>&quot;><BR>

3) I can get the image size of the images uploaded to a temporary location, but they are not displaying in my preview of the article. Can I display them from the temporary location or do I have to move them to a permanent location first? Is there a way I could display them from their original location (i.e. user's hard drive)?

Thanks in advance for any help.

 
1) Textareas' values are set between the tags, like <textarea...>INSERT VALUE HERE</textarea>
2) When the input's type is file, you can't specify a value for it.
3) You would have to move it to a permanent location or write a script that retrieves the image from the temporary directory and prints it to the browser. //Daniel
 
Cool!

Problem 1 is fixed. As for problem 3, I guess I could move the images to a permanent folder (called /temporaryimages or something). The whole idea of writing a script to grab them from the temporary upload location sounds better, though.....as I won't have to worry about multiple writers writing at the same time and getting in each other's way. Does anyone have any idea how I'd go about that or a good place I could start looking for info?

Does anyone else have any ideas how to deal with problem 2? I will use some other than a file input if I have to. I want it to do all of the following:

a) have a browse button for finding a file
b) uploading that file
c) being able to put the file location back into the box when the page calls itself
 
Here is how you would do number 3.
In your current script, you make the image tag(s) point to getimage.php?file=$file, where file is set with the following lines:
Code:
$file = preg_replace(&quot;!^/tmp/(.*)!&quot;, &quot;\\1&quot;, $_FILES['pic1']['tmp_name']);
and in the getimage.php you would have something like this:
Code:
<?php
header('Content-type: image/IMAGE TYPE HERE');
readfile(&quot;/tmp/&quot; . $_GET['file']);
?>
IMPORTANT: You should add a lot more security to this script, since it is a HUGE security risk the way it is now. //Daniel
 
Ok...I've got everything working except for one little thing.

If I do the following:

-enter info and location of images
-hit preview button
-change one of the images to upload
-hit preview again

It will still display the initial image unless I hit the refresh button on my browser. I decided to go the route of storing the images in a preview folder and looking at them from there. The actual src URL for the image never changes...the new image overwrites the old one. I'm guessing my browser is caching the original image, doesn't see a new url, and assumes it's the same image. I know this must be really simple, but how do I force it to load the new image without making the end-user have to hit refresh?

Thanks a ton.

Ryan
 
Thanks for the help, but I tried that and got the same incorrect results. I put the clearstatcache() statement a little before I echo'ed out my <img> tags. Is there a specific place it has to go? Also, I read the info on that page and it doesn't seem like this statement is for clearing the browser's cache. Isn't that what I want to do?
 
In case anyone is stuck with the same problem, I found the solution in the faq section.

Sticking a randomly generated number onto the src will make the browser pull the image from the server all the time. EXAMPLE: <img src=&quot;pic1.jpg?t=23234&quot;> where 23234 is randomly generated every time. This seemed like a sloppy work around to me.

Another person wrote in and suggested appending the result of filemtime(&quot;pic1.jpg&quot;) in place of a randomly generated number. This means the image will use the cached version when the image hasn't been modified and be pulled from the server when it has been modified. This worked perfectly for me. Hope it helps someone else as much as it helped me.
 
clearstatcache should be used to clear all file calls before any new ones are made, however (and I was a little slack while posting last night) the code provided on the clearstatcache link to php.net shows more accurately how to clear teh browser cache completely.

It depends if you use alot of file calls (you don't) or if you need to clear the browser cache (you do).

Check the code posted as examples on that link, I think its more fitting to your needs. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top