File upload

This is a discussion on "File upload" within the PHP Forum section. This forum, and the thread "File upload are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Jan 10th, 2006, 16:06
Reputable Member
Join Date: Nov 2005
Posts: 127
Thanks: 0
Thanked 0 Times in 0 Posts
File upload

Hi, im trying to upload an image using php - ive looked at numerous tutorials and think i know how to do it except i think im missing something basic

to my understanding the following code should output the path that the user enters in the upload form


PHP: Select all

$image_file $_FILES[’upload_file’]; //the name of the file from the form
echo "Image_file = $image_file"
however its just outputting Image_file =

the name of the upload field in the upload form is "upload_file" and im trying to do this to test why everytime i try to write code to upload the file, it fails. I've even tried to use scripts that are already done and have set the upload directory to 777, though I wasn't aware that this would be needed for the above code to do its job
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 10th, 2006, 17:40
Reputable Member
Join Date: Nov 2005
Posts: 127
Thanks: 0
Thanked 0 Times in 0 Posts
Re: File upload

I understand it now its
$image_file = $_FILES[upload_file][name]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 10th, 2006, 19:20
Reputable Member
Join Date: Nov 2005
Posts: 127
Thanks: 0
Thanked 0 Times in 0 Posts
Re: File upload

could anyone tell me whats wrong with this - ive set Image folder to 777

PHP: Select all

<?php
$error_message 
""//error upload message, set to zero
$Image_file $_FILES[upload_file][name]; //the name of the image  $Image_size = $_FILES[upload_file][size]; //the size of the image
$Image_type $_FILES[upload_file][type]; //the type of the image
$Temp_directory $_FILES['upload_file']['tmp_name']; //the temp directory
$Target_directory "www/Image/";
$Target_directory $Target_directory basename$_FILES['upload_file']['name']); 

echo 
"The name of the file from the form is $Image_file <br>";
echo 
"The size of the image is $Image_size <br>";
echo 
"The type of file uploaded is $Image_type <br>";
echo 
"The temporary path is $Temp_directory <br>";
echo 
"The target path is $Target_directory <br>";

if ( !
ereg"gif|png|x-png|jpeg"$_FILES[upload_file][type]) ) {
   echo 
"This is not an image";

  else if ( 
$_FILES[upload_file][size] > 110000 ) {
   echo 
"This file is too large";


copy ($_FILES['upload_file']['tmp_name'], "$Target_directory"
    or die (
"Could not copy"); 

?>
it gives the following output when i try to load a small image


The name of the file from the form is bnr.png
The size of the image is
The type of file uploaded is image/x-png
The temporary path is /tmp/phpMBYQBS
The target path is www/Image/bnr.png
Could not copy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 11th, 2006, 17:48
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: File upload

Is the target path really relative to your current directory, or should you have a ../ or something in front of it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 11th, 2006, 18:25
Reputable Member
Join Date: Nov 2005
Posts: 127
Thanks: 0
Thanked 0 Times in 0 Posts
Re: File upload

I didnt think of that, i bet I should have ../ at the front. I sorted it now anyway (by putting the script into the folder I want to upload images to and just using the file name as the second parameter)

Would you know of an alternative to setting the directory to 777, im bit worried that this is unsafe, im only using the upload for me at the minute (not public) but if I want to allow this facility on the actual site then this from what ive read would be unsafe.

Also I read somewhere about using explode() to make sure that the file being uploaded is an image and not a malicious file with an image extension - would you recommend I use this?

thanks,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
file, upload

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP+SQL+File Upload joshlindem PHP Forum 5 May 10th, 2008 16:48
file upload berry05 Job Opportunities 0 Mar 19th, 2008 19:09
renama file name before upload mcnika PHP Forum 1 Mar 17th, 2008 12:13
File Upload magiccupcake PHP Forum 1 Jan 22nd, 2007 05:10
simple file upload? jimmy_d PHP Forum 3 May 19th, 2006 18:27


All times are GMT. The time now is 14:39.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42