daymoon
Premium Member
Registered: 1st Aug 08
Location: Selby, North Yorkshire
User status: Offline
|
Trying to make a simple website for my uni project and got to use php..
WHat i need is a smple script for following:
User uploads picture into a form
picture appears on the screen next to form for user to make sure they uploaded correct picture..
HELP!
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
I use this script: http://valums.com/ajax-upload/
It's a nice and tidy way of doing things...
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
You can do a very basic file upload using the form file input (type='file') and then use $_FILES[] to save the uploaded file that you can recall (ie: display) later - http://www.tizag.com/phpT/fileupload.php
Ed's posted a pretty decent JQuery upload, you still deal with $_FILES on the php side of things but it makes the client side easier.
Personally i've always used a combo of JQuery and Flash (something like uploadify) as it allows more control of filesize and allowed extentsions, plus file progress (although you can do this via AJax i believe) and file queuies (plus a load more call backs). PHP side of things is pretty much the same as all PHP uploads and client side is all handled via JQuery.
If you stuck with the PHP side of things, you basically want to check to see if a file has been uploaded ($_FILES['yourfileinputname']['name'] gives original filename or $_FILES['yourfileinputname']['size'] gives file size), if it has then you want to move the file from the temporary directory (use move_uploaded_file($_FILES['yourfileinputname']['tmp_name'], 'directorylocation')) and if that is successful then you basically echo out the location of the moved file to a IMG tag.
http://www.php.net/manual/en/features.file-upload.post-method.php
|
daymoon
Premium Member
Registered: 1st Aug 08
Location: Selby, North Yorkshire
User status: Offline
|
Sorry, maybe i wasnt clear.
I have a working file upload script, what i want is when i have uploaded an image, for it to be displayed next to form...
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
Can you not just write it yourself?
In your form create a space for an image which doesn't have any code inside it but is styled to hold the space.
eg. <div style="width:250px; height:250px"></div>
Then with Ajax as Dom has described write in the img tags after the image has been uploaded in the background.
Thats what I'd do. It'd take me longer to find the existing code on the net than to code it
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
There are two ways of doing it. Synchronously using pure PHP, so you'd upload the file then on page reload after the upload had commenced you would use some PHP to display the image. You could do it Asynchronously using Javascript. The script I suggested will do what you're trying to do, and in fact I have an avatar upload script which does exactly what you're trying to do. The user uploads their avatar, the upload script handles the upload along with the PHP stuff to do the file handling and when the upload is complete the PHP script returns some JSON to say that it's all worked and tells use the new filename. The upload script then uses that JSON message to figure out if something has gone wrong or if the pic has uploaded. If the pic has uploaded you use document.getelementbyid.('image_id').src= to change the src of the existing image to the new image.
|
xa0s
Banned
Registered: 4th Mar 08
Location: Dartford, Kent Car: Turbo'd Fabia vRS
User status: Offline
|
Why complicate it with Ajax? When the file is submitted it changes page anyway so just display the image. The hard bit is already done...
Post your current code so we can see what you've done.
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
It's hardly complicating it. The script is all in here give or take a few additions... http://valums.com/ajax-upload/
|
Dom
Member
Registered: 13th Sep 03
User status: Offline
|
quote: Originally posted by daymoon
Sorry, maybe i wasnt clear.
I have a working file upload script, what i want is when i have uploaded an image, for it to be displayed next to form...
Once you've moved the file (from temp directory to root or wherever), then simply echo out an IMG tag with the image in it, eg: echo "<IMG src=\"",$filelocation,"\">"; (commas on echos are fine by the way)
If you already have a basic PHP upload then i wouldn't worry about AJAX or JQuery/Flash uploads, can always add them in afterwards if it will get your more marks.
|
xa0s
Banned
Registered: 4th Mar 08
Location: Dartford, Kent Car: Turbo'd Fabia vRS
User status: Offline
|
quote: Originally posted by ed
It's hardly complicating it. The script is all in here give or take a few additions... http://valums.com/ajax-upload/
No, it's definitely complicating it... If he has to ask for help doing something simple like this then introducing Ajax as well isn't a great idea.
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
For starters it's not even AJAX. You couldn't use AJAX in that situation even if you wanted to.
|