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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regarding uploading and viewing images

Status
Not open for further replies.

cs7536

Programmer
Joined
Apr 20, 2003
Messages
130
Location
US
mysql & php on apache.

I would like to upload an image to a directory but have the name i think in the database and then view the image on a page. I would like for it to be with id so that they can be viewed with an article. something like californiaairbus.com monthly special area, but in php instead of cfm. I dont know where to start can someone help please.
 
u need to first get ur basics in php-database mgmt. do u need help there also? if u do the i suggest u check the FAQs. its pretty easy once u learn to run sql.

Known is handfull, Unknown is worldfull
 
I have all my table in tacked and ready.. mysql is what I am using, my problem is the php code at this point.

Thanks
Max
 
First, I recommend that you take a look at the PHP online manual under "Handling File Uploads":
The example code there will get you started learning how to accept file uploads.

You might also take a look at the online manual under mysql_fetch_array(): , paying attention to the example code there.

Want the best answers? Ask the best questions: TANSTAAFL!
 
I raed that documentation, What I got out of that was the following.

I created a file upload script that allows me to upload a file to a selected directory but it uploades it as upload.jpg then erases it from the original location afther copying the file to directory. but if I try to upload another then it replaces the previous upload because it names all the uploads the same.

Next is I created a script to display the file but when I view it it diplayes a bunch of 172b2bhvd"#(#()##>#: looking stuff , what appearse to be the code that composes the image but no image physicly viewable.

yet I cant get it to input the name of the file in the database for so that it can point to the image for access to display on viewing page.

I am stuck.
Being a newbie is tuff. but I am working hard at it.
 
You aren't going to be able to output the content of a JPEG to an HTML page and see anything bug gobbledygook. If you create an HTML page with a link to that graphic, and then click on that link in a browser, you will have better results.

The array $_FILES will have an element that contains the original file name as you uploaded it from your filesystem. If you upload two files of the same name, than overwriting is expected behavior.

Want the best answers? Ask the best questions: TANSTAAFL!
 
this is the code to my html page:

<body>
<form action=&quot;upload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; name=&quot;upload_image&quot;>
Submit this file:
<input type=&quot;file&quot; name=&quot;userfile&quot;><br>
<input type=&quot;submit&quot;>
</form>
</body>

this is the code to mt php page:

<?php
// copy the file to c:\upload.txt. remember to escape backslashes!
if (copy($userfile, &quot;images\upload.jpg&quot;)) {
echo (&quot;<B>File successfully copies!</B>&quot;);
} else {
echo(&quot;<b>Error: Failed to copy file...</b>&quot;);
}

// destory the file now we've copied it
unlink ($userfile);
?>

but even if I upload a file with a different name it replaces.
 
Of course. You're using a hard-coded destination filename in your code.

Please examine the &quot;Handling file uploads&quot; link again. Pay particular attention to what is made available to you in the $_FILES array.

Try a simple
print_r($_FILES);
in your upload script to see what PHP puts in there.

Want the best answers? Ask the best questions: TANSTAAFL!
 
ok, I appreciate your help but I will have to step out for a few hours to attend a family gathering.

be back soon.
Max
 
I am not understanding, please re-explain.

Thanks
 
I need help getting this done today if someone can help please. but can those assisting me please explain in simple english I am still new to php code/teck terms.

thank you
 
would it be safe to say:
<?php
// copy the file to c:\upload.txt. remember to escape backslashes!
if (copy($userfile, &quot;images&quot;)) {
echo (&quot;<B>File successfully copies!</B>&quot;);
} else {
echo(&quot;<b>Error: Failed to copy file...</b>&quot;);
}

// destory the file now we've copied it
unlink ($userfile);
?>

 
I no that did not work for me, I am trying to figure it out but in a way I am guessing and that is not good. I have bought like 4 books but they explain how to post this way but obiously this is not the way that I am trying to do it. or I think posting with the file name is part of what I am trying to do. discuraging.
 
I cannot answer that. Is writing every uploaded file to the same filename correct behavior for your application?

If not, then make use of the data in $_FILES. Concatenate local filesystem path information with the uploaded filename.

Again, this page: has some good information. Pay attention to the example code given there. Also, take a look at move_uploaded_file()
Want the best answers? Ask the best questions: TANSTAAFL!
 
ok I will read carfully the links that you recommend, then I may post for assistance. I like your way of teaching you never give the answers cause how can someone learn if the answers are given to them.

thanks
Max
 
I added this script to a page for file upload:

<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES.
// In PHP earlier then 4.0.3, use copy() and is_uploaded_file() instead of move_uploaded_file

$uploaddir = '/images/';

print &quot;<pre>&quot;;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
print &quot;File is valid, and was successfully uploaded. Here's some more debugging info:\n&quot;;
print_r($_FILES);
} else {
print &quot;Possible file upload attack! Here's some debugging info:\n&quot;;
print_r($_FILES);
}

?>

and this is what I got as resaults:

Possible file upload attack! Here's some debugging info:
Array
(
[userfile] => Array
(
[name] => sintia.jpg
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)

)


does anybody know how to correct this.

 
The problem is your use of directories. move_uploaded_file() operates on the entire filesystem, not just the document root of the current virtual web server.

So unless you have a directory at the root of your filesystem called &quot;images&quot;, the move is failing because of a non-existent directory.

Either use directory names that are relative to the directory where the script is running (ie, if the &quot;images&quot; directory is a child of the current directory use just &quot;images/&quot;, not &quot;/images/&quot;) or use absolute paths (ie &quot;/home/sites/thesite/html/images&quot;).

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top