Connecting Tech Pros Worldwide Forums | Help | Site Map

Setting up file uploads with php

Newbie
 
Join Date: Mar 2007
Posts: 15
#1: Mar 29 '07
Heya guys, i got this simple script to work and now it doesnt.Its far from completed, i know, i just want it to gogogo so i can expand it to include my preferences and a few hidden fields.
I can't tell if i screwed up the code or its a server related problem. here goes....


Expand|Select|Wrap|Line Numbers
  1.  <html> 
  2. <body>
  3. <form action="upload.php" method="post" ENCTYPE="multipart/form-data"> 
  4. <input type="file" name="file" size="30" /> 
  5. <input type="submit" value="gogogo" /> 
  6. </form> 
  7. </body>
  8. </html>
  9.  
[PHP]
$uploaddir = "/var/www/html/gallery";
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'. $_FILES['file']['name']);
};
echo "Completed";

[/PHP]

ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#2: Mar 29 '07

re: Setting up file uploads with php


[PHP]$uploaddir = "/var/www/html/gallery"; [/PHP]
remove "/" from the beginning of the path and put a "/" to the end check it.
[PHP]$uploaddir = "var/www/html/gallery/"; [/PHP]

if your files are listed with gallery folder {all are in same DIR} simply this also work.
but make sure to assign the write permission to the folder if its remote server.

[PHP]$uploaddir = "gallery/"; [/PHP]
Newbie
 
Join Date: Mar 2007
Posts: 15
#3: Mar 29 '07

re: Setting up file uploads with php


thanx for you're input ajax, i tried the different variations but to no avail, (on a remote server btw). it seems such a simple script.

[directories and permissions]

/ -- var -- 755 / www-- 755 / html -- 755 | -- gallery -- 777
| -- form.php -- 755
| -- upload.php -- 755
I just noticed that my phpinfo() returns upload_tmp_dir = novalue but file_uploads = On. is that correct?
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#4: Mar 29 '07

re: Setting up file uploads with php


Quote:

Originally Posted by jungabunga

thanx for you're input ajax, i tried the different variations but to no avail, (on a remote server btw). it seems such a simple script.

[directories and permissions]

/ -- var -- 755 / www-- 755 / html -- 755 | -- gallery -- 777
| -- form.php -- 755
| -- upload.php -- 755
I just noticed that my phpinfo() returns upload_tmp_dir = novalue but file_uploads = On. is that correct?

Put your form.php,upload.php and gallery in side one dir we will say www dir.then set your variable like this.
[PHP]$uploaddir = "gallery/"; // Note only single "/"[/PHP]

make sure to set 777 for gallery dir and give it a try again.
and remove the file size="30" from the form for now.
Newbie
 
Join Date: Mar 2007
Posts: 15
#5: Mar 29 '07

re: Setting up file uploads with php


[quote=ajaxrand]Put your form.php,upload.php and gallery in side one dir we will say www dirQUOTE]
thx ajax, but my root.dir is html. i put them i a subfolder, rechecked my permissions, still no go. tried the new path, still no go, tried variations of the path, still no go. its hard to believe it actually worked.
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#6: Mar 29 '07

re: Setting up file uploads with php


[quote=jungabunga]
Quote:

Originally Posted by ajaxrand

Put your form.php,upload.php and gallery in side one dir we will say www dirQUOTE]
thx ajax, but my root.dir is html. i put them i a subfolder, rechecked my permissions, still no go. tried the new path, still no go, tried variations of the path, still no go. its hard to believe it actually worked.

Sorry I didn't get u.Is it working or Not?
Newbie
 
Join Date: Mar 2007
Posts: 15
#7: Mar 29 '07

re: Setting up file uploads with php


no sry, no good so far
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#8: Mar 29 '07

re: Setting up file uploads with php


Try this one just execute this after creating directory named myfiles with 777.
put php script and myfiles folder in same place.

[PHP]<?php
$target_path = "myfiles/"; //Directory Name to Upload
if($_POST['upload'])
{
$newName = md5(basename($_FILES['upload_fle']['name']));
$ext = strrchr(basename($_FILES['upload_fle']['name']), ".");
$fullName = $newName.$ext;

if(move_uploaded_file($_FILES['upload_fle']['tmp_name'], $target_path.$fullName))
{
echo "The file ". basename( $_FILES['upload_fle']['name']).
" has been uploaded with this name : ".$fullName;
} else
{
echo "There was an error uploading the file, please try again!";
}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="<? echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="frmAlbum" id="frmAlbum">
<input name="upload_fle" type="file" id="upload_fle"/>
<BR>
<input name="upload" type="submit" value="Upload">
</form>
</body>
</html>[/PHP]
Newbie
 
Join Date: Mar 2007
Posts: 15
#9: Mar 29 '07

re: Setting up file uploads with php


cheers, but i got the

There was an error uploading the file, please try again!
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#10: Mar 29 '07

re: Setting up file uploads with php


This script is working in my web server. even your one also working. if it is not working in your end its your bad luck.BTW what type of files you tried to upload.
Don't go for massive files, just upload simple text files first of all. there are limitation for huge files from the php level.
Newbie
 
Join Date: Mar 2007
Posts: 15
#11: Mar 30 '07

re: Setting up file uploads with php


yeah im only trying small files. i wonder what i did. i might just reinstall web then, thx for the persistance dude.
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#12: Mar 30 '07

re: Setting up file uploads with php


Quote:

Originally Posted by jungabunga

yeah im only trying small files. i wonder what i did. i might just reinstall web then, thx for the persistance dude.

Don' t give up, give it a try. hope you will get the solution. ;)
Newbie
 
Join Date: Mar 2007
Posts: 15
#13: Mar 30 '07

re: Setting up file uploads with php


Quote:

Originally Posted by ajaxrand

Don' t give up, give it a try. hope you will get the solution. ;)

I musta screwed up the permissions on teh server or something. i resinalled my micro version of fedora and it works fine now :P
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Location: Colombo
Posts: 1,440
#14: Mar 30 '07

re: Setting up file uploads with php


Quote:

Originally Posted by jungabunga

I musta screwed up the permissions on teh server or something. i resinalled my micro version of fedora and it works fine now :P

Great work.. :) c u l8r.
Reply