TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-23-2009, 09:46 AM   #1 (permalink)
The Wanderer
 
lrichmond's Avatar
 
Join Date: Jan 2009
Location: Leeds
Posts: 8
Thanks: 2
lrichmond is on a distinguished road
Default FIle permissions causing problems

Hi folks

here is my problem, I have built a kind of repository for a client where the client can upload PDF's and then the users can download these to read as and when they want, initially this was working fine, but now I appear to have come up against a problem whereby the files will only download if the file permissions are set to full accesss, otherwise clicking the download links refers you to a page saying access denied.

I know that it is the file permissions as when I download the file via ftp, change the permissions and upload it again the file downloads without error.

here is my upload form:
Code:

<?php
if(isset($_POST['save']))
{
	$title   = $_POST['title'];
	
	
	//$date = date(“Y-m-d”);
  //	$content = $_POST['content'];
	$target_path = "/home/d/a/davidsondemo/public_html/downloads/";
	$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
	
	
	if( is_uploaded_file($_FILES['uploadedfile']['tmp_name']) ) {
	
		if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
		{
		    echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
	
			include '../inc/config.php';
			include '../inc/opendb.php';
	
			$query = "INSERT INTO news (title, created_at, filepath) VALUES ('$title', '$date', '$target_path')";
			mysql_query($query) or die('There was an error uploading the file, please try again!');
			include '../inc/closedb.php';
	
			echo "<span class='bold_add'>Article '$title' added</span>";
		} else {
		    echo "There was an error uploading the file, please try again!";
		}
	} else {
		echo "The file could not be uploaded to the tmp directory, please try again.";
	}
}



?><br /><br />
	 Please enter the news item and click save article
<form method="post" enctype="multipart/form-data">
  <table width="312" border="0" cellpadding="0" cellspacing="0" class="box" align="center">
    <tr> 
      <td width="40" class="white">Title</td>
      <td width="272"><input name="title" type="text" class="text" id="title" /></td>
    </tr>
    <tr> 
      <td width="40" valign="top" class="white">Add news article	</td>
      <td><input type="file" class="text" name="uploadedfile" /></td>
    </tr>
    <tr> 
      <td width="40">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr> 
      <td colspan="2" align="right" ><input name="save" type="submit" class="btn" id="save" value="Save Article"></td>
    </tr>
  </table>
</form>
and here is the code for the page where the users can download the pdf's:
Code:
<?php

include 'inc/config.php';
include 'inc/opendb.php';



/*
	This is the directory where we store the cache file,
	you can change it any directory you want as long as
	PHP can write to it.
*/ 
$cacheDir  = dirname(__FILE__) . '/cache/';

/*
	Generate the cache filename, the cache name is simply
	an underscore followed by the article id. In case this
	script is called without any id (i.e. we want to show
	the article list ) then use index.html as the cache name
*/
if (isset($_GET['id'])) {
	$cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
} else {
	$cacheFile = $cacheDir . 'index.html';
}	

/* 
	If we found the the cache file
	read it and send to the client
*/
if (file_exists($cacheFile))
{
	header("Content-Type: text/html");
	readfile($cacheFile);
	exit;
}

/*
If the script reaches this point, then the cache file
is not found. Now we fetch the info from the database
*/

	
	$query  = "SELECT id, title, filepath FROM news ORDER BY id DESC";
	
	include 'inc/paging.php'; 
	$page = new paging(); 
	$page->sql = "$query"; 
	$page->rowsPerPage = 12; 
	$page->pageID = $_GET['pid'];
	$query_result = $page->paging_query();
	
	
	$result = mysql_query($query) or die('Error : ' . mysql_error()); 
	list($id, $title, $filepath) = $row;
	
	


	$content =  '<ul>';
		$result = mysql_query($query) or die('Error : ' . mysql_error()); 
		while ($row = mysql_fetch_array($query_result))
<?php

include 'inc/config.php';
include 'inc/opendb.php';



/*
	This is the directory where we store the cache file,
	you can change it any directory you want as long as
	PHP can write to it.
*/ 
$cacheDir  = dirname(__FILE__) . '/cache/';

/*
	Generate the cache filename, the cache name is simply
	an underscore followed by the article id. In case this
	script is called without any id (i.e. we want to show
	the article list ) then use index.html as the cache name
*/
if (isset($_GET['id'])) {
	$cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
} else {
	$cacheFile = $cacheDir . 'index.html';
}	

/* 
	If we found the the cache file
	read it and send to the client
*/
if (file_exists($cacheFile))
{
	header("Content-Type: text/html");
	readfile($cacheFile);
	exit;
}

/*
If the script reaches this point, then the cache file
is not found. Now we fetch the info from the database
*/

	
	$query  = "SELECT id, title, filepath FROM news ORDER BY id DESC";
	
	include 'inc/paging.php'; 
	$page = new paging(); 
	$page->sql = "$query"; 
	$page->rowsPerPage = 12; 
	$page->pageID = $_GET['pid'];
	$query_result = $page->paging_query();
	
	
	$result = mysql_query($query) or die('Error : ' . mysql_error()); 
	list($id, $title, $filepath) = $row;
	
	


	$content =  '<ul>';
		$result = mysql_query($query) or die('Error : ' . mysql_error()); 
		while ($row = mysql_fetch_array($query_result))

	
	{
		list($id, $title, $filepath) = $row;
		
		$filepath = substr_replace($filepath, "", 0, 35 );
		
		$content .= "
		<li>
		<h4>$title</h4>
		<a href=\"$filepath\">click to download</a> 
		<br />
		<br/ >
		</li>\r\n";
	}
	
	$content .= '</ul>';
	
include 'inc/closedb.php';

// start output buffering ( we need the generated html
// page to create the cache file )
ob_start();

?>
<!-- start news section-->
<ul>
	<?php
echo $content;
echo $page->page_links(); 
{ 

 ?>      
<?php
}
?>
  <!-- end news section-->
	
	{
		list($id, $title, $filepath) = $row;
		
		$filepath = substr_replace($filepath, "", 0, 35 );
		
		$content .= "
		<li>
		<h4>$title</h4>
		<a href=\"$filepath\">click to download</a> 
		<br />
		<br/ >
		</li>\r\n";
	}
	
	$content .= '</ul>';
	
include 'inc/closedb.php';

// start output buffering ( we need the generated html
// page to create the cache file )
ob_start();

?>
<!-- start news section-->
<ul>
	<?php
echo $content;
echo $page->page_links(); 
{ 

 ?>      
<?php
}
?>
  <!-- end news section-->
I am still very much a newbie when it comes to PHP so if someone could help me in any way possible I would be most grateful :)

TIA

L.Richmond
lrichmond is offline  
Reply With Quote
Old 01-23-2009, 01:53 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about trying to modify the permissions once the file has been uploaded? Just above where it says the file has been successfully uploaded, try the following snippet of code:

php Code:
chmod($target_path, 0777);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-23-2009, 02:09 PM   #3 (permalink)
The Wanderer
 
lrichmond's Avatar
 
Join Date: Jan 2009
Location: Leeds
Posts: 8
Thanks: 2
lrichmond is on a distinguished road
Default Win!!

thats perfect thank you so much Wildhoney :D :D
lrichmond is offline  
Reply With Quote
Old 01-26-2009, 03:17 AM   #4 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

File permissions in PHP do have a commonalities with *nix file permission. For further references try this references or simply google file permissions in php :)

*nix: *Nix File Permission
php: Php File Permission
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB 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
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 4 05-04-2011 06:11 AM
Where is my file? superthin General 3 07-25-2009 09:48 AM
Aptana Jaxer and file uploads xenon Advanced PHP Programming 2 06-06-2008 10:22 AM
Writing to XML file buildakicker General 8 02-06-2008 08:17 PM
Make Your Own PHP Flash Player Which You Can Pass a File Variable with Preview!!!! thegrayman Tips & Tricks 2 12-12-2007 12:44 AM


All times are GMT. The time now is 02:21 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design