Advertisement

05.07.2008 at 11:35AM PDT, ID: 23383828 | Points: 500
[x]
Attachment Details
file upload using browse button
I have a basic PHP email form on my site right now... I am trying to figure out how to add a field that allows users to browse and upload files that then are uploaded into a specified folder on my server when the form is submited.

Also, I want to make sure that I can restrict the file type to .zip and .jpeg.

Thank you.
Start your free trial to view this solution
Question Stats
Zone: Web Development
Question Asked By: glopicon
Question Asked On: 05.07.2008
Participating Experts: 2
Points: 500
Views: 0
Translate:
Loading Advertisement...
05.07.2008 at 11:40AM PDT, ID: 21519190

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 11:56AM PDT, ID: 21519313

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 02:20PM PDT, ID: 21520444

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 02:32PM PDT, ID: 21520527

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 02:39PM PDT, ID: 21520567

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 02:49PM PDT, ID: 21520635

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 03:23PM PDT, ID: 21520872

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 03:41PM PDT, ID: 21520984

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 04:10PM PDT, ID: 21521149

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 04:17PM PDT, ID: 21521193

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 11:35AM PDT, ID: 21527260

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 11:37AM PDT, ID: 21527269

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 09:06AM PDT, ID: 21534248

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.07.2008 at 11:40AM PDT, ID: 21519190

Rank: Master

Here's a very simple example.....call this index.php
<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

Now call the next upload.php
<?php

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";

?>

Where you have to change $uploaddir to your own directory that is writable.
 
05.07.2008 at 11:56AM PDT, ID: 21519313

Rank: Sage

Try this:
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:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script type="text/javascript"><!--
function checkFile(o)
{
	if( o.value && !/\.(zip|jpeg|jpg)$/i.test(o.value) )
	{
		alert( "Only zip and jpeg files are allowed");
		return false;
	}
	else if( o.value=="" )
	{
		alert("You did not specify a file.");
		return false;
	}
return true;
}
//--></script>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST" onsubmit="return checkFile(uploadedfile);">
	<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
	Choose a file to upload: <input name="uploadedfile" id="uploadedfile" type="file" onchange="checkFile(this)" /><br />
	<input type="submit" value="Upload File" />
</form></form>
</body>
</html>
Open in New Window
 
05.07.2008 at 02:20PM PDT, ID: 21520444
Where do I specify where the file will be uploaded to the server?
 
05.07.2008 at 02:32PM PDT, ID: 21520527

Rank: Sage

>>Where do I specify where the file will be uploaded to the server?
<input type="file"> automatically puts a text field with a browse button (for file selection) on your page.
the action attribute of the <form> tag indicates where the file should be uploaded to. In the example I posted, action="uploader.php", so the file will be uploaded to uploader.php. You need to specify the path to your script instead.
 
05.07.2008 at 02:39PM PDT, ID: 21520567
Xyptilon2 - here is the error I keep getting now... any clues? I think I might be getting the path wrong some how... I DID create the folder on the server called /uploads/.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Warning: move_uploaded_file(/var/www/uploads/Winter.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /data/15/1/50/80/1376895/user/1477063/htdocs/uploader.php on line 8
 
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/data/tmp/phpjWUKQo' to '/var/www/uploads/Winter.jpg' in /data/15/1/50/80/1376895/user/1477063/htdocs/uploader.php on line 8
Upload failed
 
Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => Winter.jpg
            [type] => image/pjpeg
            [tmp_name] => /data/tmp/phpjWUKQo
            [error] => 0
            [size] => 105542
        )
 
)
Open in New Window
 
05.07.2008 at 02:49PM PDT, ID: 21520635

Rank: Sage

It sounds like your server configuration does not let php access folder outside of the document root. For purposes of debuggin, try creating the upload folder in htdocs then modify:
$uploaddir = '/var/www/uploads/';

to this:
$uploaddir = '/data/15/1/50/80/1376895/user/1477063/htdocs/uploads/';
 
05.07.2008 at 03:23PM PDT, ID: 21520872
Its going to take some time to get access to the /htdocs/ folder since I currently only have FTP access to the /public_html/ folder...

I dont fully understand this part of the original path you suggested: /var/www is it possable this is just wrong?

 
05.07.2008 at 03:41PM PDT, ID: 21520984

Rank: Sage

>>Its going to take some time to get access to the /htdocs/ folder since I currently only have FTP access to the /public_html/ folder
Well, based on your error description above:
...in /data/15/1/50/80/1376895/user/1477063/htdocs/uploader.php

uploader.php is in your htdocs folder already. So if you can ftp the uploader.php file to your server, then within the folder where you have uploader.php you should create the folder uploads and the previous suggested path should work. So if you were to upload image1.jpg, your file structure would be:
-uploader.php
 -uploads/image1.jpg

>> is it possable this is just wrong?
That's for you to answer, after all you said "... I DID create the folder on the server called /uploads/" so you should know where you created the folder.
 
05.07.2008 at 04:10PM PDT, ID: 21521149
This is what I have access to: /htdocs/public_html/

What should I do next?
 
05.07.2008 at 04:17PM PDT, ID: 21521193

Rank: Sage

on the folder where you have the uploader.php script, create the folder uploads then use:
$uploaddir = '/data/15/1/50/80/1376895/user/1477063/htdocs/uploads/';
 
05.08.2008 at 11:35AM PDT, ID: 21527260
It says its uploading the image, but when I check the server - the file is not there... Here is the message that comes up now:

File is valid, and was successfully uploaded.

Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => Sunset.jpg
            [type] => image/pjpeg
            [tmp_name] => /data/tmp/phpOgPqQL
            [error] => 0
            [size] => 71189
        )

)

Also, attached is what I have for file uploads.php
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:
<?php
 
$uploaddir = '/data/15/1/50/80/1376895/user/1477063/htdocs/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
 
echo "<p>";
 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}
 
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
 
</head>
<body>
FILE UPLOADED!!!
<form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
</body>
</html>
Open in New Window
 
05.08.2008 at 11:37AM PDT, ID: 21527269
Attached is the code for test3.php which is the first page users will go to to upload.

Am I doing this right?

- Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
 
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
</body>
</html>
Open in New Window
 
05.09.2008 at 09:06AM PDT, ID: 21534248

Rank: Sage

Try getting rid of basename:
<?php
 
$uploaddir = '/data/15/1/50/80/1376895/user/1477063/htdocs/uploads/';
$uploadfile = $uploaddir . ($_FILES['userfile']['name']);
 
...
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628