PHP File Upload - Server File Permissions Issue

Posted on 2011-03-24
Last Modified: 2012-05-11

I have had a sudden increase in users of my wordpress plugin complaining about permission related errors. I'm no pro yet but my first stance on the matter was that it is the servers restrictions. I'm still not convinced that it is my plugin but it just seems a little strange that a server would not allow the uploading of a text or csv file. Plus it has never come up before and suddenly multiple people  report it.

Warning: move_uploaded_file(/home/httpd/html/ [function.move-uploaded-file]: failed to open stream: Permission denied

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpMvJ9cb' to '/home/httpd/html/

One permission that the plugin didn't have was to open a csv file for writing.

I will show you my method for uploading a file, maybe you can suggest another approach or even just help me decide where the fault is. My script or the users server. I should say I build the plugin on my computer, no problems there and I have many test blogs online, no problems their either.

Troubleshooting has been going on for days regarding this so any help is appreciated.

function eci_csvupload( $upload,$set )
	//$_POST = stripslashes_deep($_POST);
	$upload = $_FILES['file'];
	// check for errors
	if ( $upload['error'] != 0 ) 
		eci_log(__('Upload Failed'),__('A new file upload attempt failed to start'),'Error',$set,'Failure' );	
		eci_err(__('Upload Failed To Start'),__('CSV file upload could not be started 
		at all as the file loader returned error, please try again.'));
	elseif ( $upload['error'] == 0 ) 
		// get path data
		$pat = get_option('eci_pat');
		// if no path data or path not submitted use default
		if( !$pat || !isset( $_POST['eci_path'] ) )
			$path = 'default';
			// install path array
			// now get paths data
			$pat = get_option('eci_pat');
			$path = $_POST['eci_path'];

		// now get the actual path
		foreach( $pat as $key=>$p )
			if( $path == $key )
				$path = $p['path'];
		// confirm path is valid else exit
		$openresult = opendir( $path );
		// if failed to open directory display error
		if( !$openresult )
			// use directory name
			// add manual directory creation button to error message
			$createform = '
			<form method="post" name="eci_createdirectory" action=""> 
				<input name="eci_pathdir" type="hidden" value="'.$path.'" />
				<label>Enter Directory Name:<input name="eci_pathname" type="text" value="" size="15" maxlength="15" /></label>
				<input class="button-primary" type="submit" name="eci_createdirectory_submit" value="Create Directory" />
			eci_err(__('Failed To Open Path/Directory'),'The path being used for uploading your
					csv file does not appear to be a valid directory or a directory with
					permissions that will allow the upload. Your CSV file was not uploaded. 
					Here is the directory you are attempting to upload your csv file to and
					a button to create it manually.<br /><br />
					<strong>Required Path</strong>'.$path.'<br /><br />
			// build final file path
			$path = $path.$upload['name'];
			// if the final path already exists, delete the existing file then continue
			if ( file_exists( $path ) ) 
				// change file exists switch to true
				$fileexisted = true;
				// get existing files datestamp
				$oldtime = filemtime( $path );

				// delete the existing file
				$deleted = unlink( $path );
				if( $deleted )
					eci_err( __('Existing File Deleted'),__('A matching csv filename was found in the
							same directory you are uploaded to. It has been deleted as part of
							the upload process.') );
				$fileexisted = false;
				$deleted = true;// set variable only, has no purpose
			// if file could not be delete do not continue and let user know
			if( !$deleted && $fileexisted === true )
				// $deleted or $fileexists do not equal true, both must be true to avoid this
				eci_err(__('File Name Exists Already'),__('You already have a CSV file with the same name
						in the selected directory. The plugin could not delete it. You will need to
						delete it manually then try again.'));
			elseif( $fileexisted === true && $deleted == true || $fileexisted === false )
				// move temp upload to its final path
				$moveresult = move_uploaded_file( $upload['tmp_name'], $path );
				// alert user if file move failed
				if( !$moveresult )
					eci_err(__('File Failed To Upload'),'There is no clear reason for the failure.
							The plugin confirm no file with the same name exists. Please check
							the directory permissions and ensure you are uploading a correctly
							formatted CSV file. '. $moveresult .' and report this problem so we 
							can investigate. Please considering using FTP.');
					// confirm file has uploaded to the correct directory and path exists
					if ( file_exists( $path ) ) 
						eci_notifications( $upload['name'],'Uploaded a new csv file','NA',1 );

						eci_mes(__('CSV Upload Success'),'You uploaded '.$upload['name'].' and
						can now use it to create a new project or update an existing one
						using the file name.');

						eci_err(__('Possible Upload Error'),__('The plugin detected that the upload
								was a success but on double checking that the uploaded file is 
								now in place, the plugin could not locate your file. Please
								investigate and report this if the problem persists.'));

Open in new window

Question by:Ryan Bayne
LVL 34

Accepted Solution

Beverley Portlock earned 250 total points
ID: 35205738
These errors are usually caused by the webserver not having permission to write into the receiving folder. In the example you cited above I would be checking ownership and permissions on /home/httpd/html/

The usual method is to grant GROUP access to the web daemon or a group that the web daemon is a member of and then allow that group to have write permission to the folder, so on a command line type

ls -l /home/httpd/html/

and look for the wp folder and note the user and group name together with the permissions and post them back here so we can help you.

Author Comment

by:Ryan Bayne
ID: 35206165
In terms of me the developer providing a plugin. Can you confirm that there is nothing I can do to get around this?

Thank you for your reply. I just want to clear this up before I start telling the occasional customer that they need to contact their hosting, nothing I can do etc.

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress themes CSS? 6 30
What is this? I've never seen this... 2 28
Duplicated data in GROUP_CONCAT 2 14
looking for wordpress theme 3 24
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question