PHP Upload Function

Hello,

This is my first post on the forum, and am glad to be hear!

Ok, I have been coding for about a year and a half and am just about fluent in HTML and CSS, so I have now decided to go down the programing side of things to pick up a few skills in PHP and javascript and what not…

I am still very new to PHP but think I have the main idea down, but am stuck on trying to figure out a couple of things…

First, I was wondering how I would go about creating an upload form for a user to upload a photo to a particular page on their site (like a photo gallery) and then have it post without having to be manually coded into the page?

I guess a better example would be like a word press blog on the admin page they allow you to create a post or upload photos etc which then get uploaded to the server and are then posted to the page automatically without any other type if user administration or interaction.

Any help on this subject would be much appreciated! Thank you!

wordpress stores the information for every uploaded image in the database which is then related to the post, so every post has its own “gallery” of course you can also just upload a picture and then use Javascript to Copy the path of the uploaded Picture into your texteditor like Wordpress does with TinyMCE.

Ok, so is this possible to do with just PHP and no javascript? Oh, and do you know what javascript would have the ability to copy the path of an uploaded picture to another one of my pages? Thanks for your help.

P.S. - Just to clarify, I write most of my stuff from scratch and don’t use wordpress.

Hehe, yeah i though that but since you started it as an example i went along with it.

And no i dont think you could do it without JavasCript at least not without a major hassle for the user.

If you use a common WYSIWYG Editor there sure is a plugin (for example i know there are a couple for tinymce which is an excellent editor).

You know, I’m in similar situation :). I found one php tutorials on http://phpforms.net/tutorial/tutorial.html. I’ll share a secret - if I need any form for a site I go there and use a free demo. :wink:

Faster tutorial:

http://www.php.net/manual/en/features.file-upload.post-method.php

Awesome, thanks guys! Now that I have the upload feature down, I have one more question…

Say I upload a pic and then want it to to post to a photo gallery in an unordered list on one of my pages? Here, I will post an example below:

  1. The user uploads a pic to a particular folder/page on the server

  2. I then would want to post to a <ul> on one of the gallery pages on the site:

<ul class="gallery">
    <li><img src="gallery_photo1"></li>
    <li><img src="gallery_photo2"></li>
    <li><img src="gallery_photo3"></li>
    <li><img src="gallery_photo4"></li>
    <li><img src="additional uploaded pics"></li>
    <li><img src="additional uploaded pics"></li>
    <li><img src="additional uploaded pics"></li>
</ul>

Is there any type of PHP function that has the ability to include contents on a particular folder on your server to a page? I am aware of the php include function, but is there any way to make it so it takes and displays every file associated with a particular folder on the server? Like so every time something is uploaded to say the gallery folder, it posts it to a <ul> on a page on the server like the code that I posted above.

Thanks for your help!

Directory functions (Please make note of Example 2 in the readdir page… eliminating the . and … are essential.)

Ok, well I tried to run the script to see if I could get it to work and am getting an error message.

Here is the script I am using to display the files in a directory:

<?php 
$dir = opendir ("/images"); 
        while (false !== ($file = readdir($dir))) { 
                if (strpos($file, '.jpg',1)||strpos($file, '.png',1) ) { 
                    echo "$file <br />"; 
                } 
        } 
?> 

and here is the error message that I am getting:

Warning: opendir(/images) [function.opendir]: failed to open dir: No such file or directory in /home/sharperi/public_html2/file.php on line 2

Warning: readdir(): supplied argument is not a valid Directory resource in /home/sharperi/public_html2/file.php on line 3

Help please :frowning:

Here: [B]How To Handle File Uploads With PHP[/B] by Kevin Yank

Thank you, but my problem isn’t uploading a file… it’s trying to upload a file and at the same time have it posted to a particular spot on one of my web sites pages like a photo gallery.

Any other help on this subject is much appreciated.

anybody else?

When the file is successfully uploaded, are you recording anything in your database?

It does not seem that OP has stored any information of the uploaded files to DB. So it would be wise to store at least the file names (or more like title of the file, description if any, etc) in the database table so that it will be easier to retrieve later in any pages in the website.

So when the image is uploaded successfully then insert a row to a table:


$filename = basename($_FILES['uploadedfile']['name']);
$path = 'uploads/';
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path . $filename)){
    mysql_query("INSERT INTO tblename SET filename='$filename', uploaded='2010-04-18'") or die(mysql_error());
}

The error says that the directory doesn’t exist, try using the full path or a relative path to your script (/ isn’t the same as html in php). You can easily just use file_exists() to do this.

For what it’s worth, you’re thinking about things the wrong way. Uploading a file doesn’t post it anywhere, it stores it on the server. From there you can get that file/image from any of your scripts that need it. It can help to use a db to manage this, but you could just as easily create files in something like /www/uploads/username per user, and then use the path to find them.

Also check out glob.

Alright, thanks so much for your help guys… – after much reading around and a more in depth study of how sql and a database work (much thanks to one of sitepoints books Build Your Own Database Driven Web Site Using PHP & MySQL, I have found the solution and understand more clearly how things are received and stored onto a DB.

Like I said before, I am still new to the programing side of things, but have already learned much more just within these last couple of days.

Again, I thank you for all of your time and help on this subject and will be sure to post any other questions I might have on this forum. Thanks again!

I’d ever come across this script that automatically generates a gallery out of the directory of photos. PHPSlideShow