File upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shruti0324
Forum Newbie
Posts: 4
Joined: Thu Dec 06, 2007 11:44 pm

File upload

Post by shruti0324 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello every one,below is the code which i wrote to upload the files to the folder and get back from it.I created a folder called upload which can store the uploaded files.After i upload the files i get the msg successfully uploaded but it wont get stored in the upload folder to get back the file.Plese let me know what is wrong in the code and how can i store it in the upload folder or any other folder.

Code: Select all

<?php

$uploaddir= "upload/";
// Where you want the files to upload to
//Important: Make sure this folders permissions is 0777!
$allowed_ext= "jpg, gif, png, pdf, html, doc";
// These are the allowed extensions of the files that are uploaded
$max_size= "750000";
// 750000 is the same as 7500kb
$max_height= "10000";
// This is in pixels - Leave this field empty if you don't want to upload images
$max_width= "10000";
// This is in pixels - Leave this field empty if you don't want to upload images

// Check Extension
$extension= pathinfo($_FILES['file']['name']);
$extension= $extension[extension];
$allowed_paths= explode(", ",$allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok="1";
}
}
// Check File Size
if ($ok=="1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big!";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
}
// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'. $_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
} else {
print "Incorrect file extension!";
}
?>
<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30"> <input type="submit" value="Upload!">
</form>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post by devendra-m »

Did you check write permission for the folder upload
shruti0324
Forum Newbie
Posts: 4
Joined: Thu Dec 06, 2007 11:44 pm

File upload

Post by shruti0324 »

Yes,i have given all the permission to the folder.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Post by markusn00b »

I can't see anything wrong with it...

even when i tested that code on my pc it worked just fine.. a little sloppy though.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

If the code works, then it's likely a problem of permissions. Sometimes, you need to give all permissions to the folder PHP will be uploading to as well as the folder that the upload folder is in. If you want to avoid the complications, actually build the folder from within PHP so that PHP already has the ownership permissions.
Post Reply