View Single Post
Old 01-23-2009, 09:46 AM   #1 (permalink)
lrichmond
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