Solved

php images and apache

Posted on 2015-01-24
6
155 Views
Last Modified: 2015-01-25
The attached file works perfectly in my apache installation if I DO NOT add any image info in the following
All the info is entered into the database correctly
If I add the image path in the following then nothing is entered into the database
I assume it is a permissions issue:
--------------------------------------
$formfile = "";
	      
	if($_FILES['art_Thumb'] && $_FILES['art_Thumb']['size'] > 0){
		$uploaddir = "images/artists/thumb/";  //folder in which to put the file
		$ori_name = $_FILES[art_Thumb][name];
		$tmp_name = $_FILES[art_Thumb][tmp_name];
		$src = imagecreatefromjpeg($tmp_name);
		list($width,$height) = getimagesize($tmp_name);
		//landscape
		if ($width >= $height) {
			$pwidth = 450;
			$newwidth = $pwidth;
			$newheight = ($height/$width)*$pwidth;
		}
		//
		//portrait
		if ($height >= $width) {
			$pwidth = $width;
			$newheight = 450;
			$newwidth = ($width/$height)*$newheight;
		}
		//
		$tmp = imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		$newfile = $uploaddir.$ori_name;
		imagejpeg($tmp,$newfile,100);
		chmod($newfile,0777);
	}
	
	$formfile1 = "";
	if($_FILES['art_Large'] && $_FILES['art_Large']['size'] > 0){
		$uploaddir1 = "images/artists/large/";  //folder in which to put the file
		$ori_name_large = $_FILES[art_Large][name];
		$tmp_name = $_FILES[art_Large][tmp_name];
		$src = imagecreatefromjpeg($tmp_name);
		list($width,$height) = getimagesize($tmp_name);
		//landscape
		if ($width >= $height) {
			$pwidth = 900;
			$newwidth = $pwidth;
			$newheight = ($height/$width)*$pwidth;
		}
		//
		//portrait
		if ($height >= $width) {
			$pwidth = $width;
			$newheight = 900;
			$newwidth = ($width/$height)*$newheight;
		}
		//
		$tmp = imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		$newfile_large = $uploaddir1.$ori_name_large;
		imagejpeg($tmp,$newfile_large,100);
		chmod($newfile_large,0777);
	}
	mysql_query("INSERT INTO art_gallery (artgal_LinkID, artgal_Name, artgal_Thumb, artgal_Large, artgal_Title, artgal_Com, artgal_com2, artgal_Med, artgal_sizeUnit, artgal_sp, artgal_sl, artgal_fsize, artgal_fsizep, artgal_fsizel, artgal_stat, artgal_Cost, artgal_Arc, artgal_Order, artgal_Group, artgal_GroupN) VALUES
		
		(
			'".$_POST[idlink]."',
            '".$_POST[art_Nme]."',
			'".$newfile."',
			'".$newfile_large."',
			'".$_POST[art_title]."',
			'".$_POST[art_com]."',
			'".$_POST[art_com2]."',
            '".$_POST[art_med]."',
			'".$selected_radio."',
			'".$_POST[im_size_port]."',
			'".$_POST[im_size_land]."',
            '".$fselected_radio."',
			'".$_POST[fim_size_port]."',
			'".$_POST[fim_size_land]."',
			'".$_POST[art_stat]."',
			'".$_POST[art_cost]."',
			'".$_POST[art_arc]."',
            '".$_POST[arc_orderS]."',
			'".$_POST[art_grp]."',
			'".$selected_radio_grp."'
			) 
		
	");
	
	//thumbnail gallery insert

Open in new window

<?php

session_start();
include('connections/example_connect.php'); 

include('connections/example_functions.php'); 

require_once('administration/RAY_EE_config.php');

$uid = access_control();
	
	if (!empty($_GET[artist_added])) {
	$artadded = $_GET[artist_added];	
	record_set('artInfo',"SELECT artinf_Name FROM art_info WHERE artinf_Name = '$artadded' ");
}
	
record_set("artThumbsInf", "SELECT * FROM art_thumbs WHERE art_Name = '$artadded' ");


$imageadd = $_GET[image_added];
record_set("artgaladd", "SELECT * FROM art_gallery WHERE artgal_Title = '$imageadd' ");

$imad = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image&nbsp;Added:&nbsp";

record_set("artNames", "SELECT * FROM art_info" );

$selname = urldecode($_GET[art_name]);
record_set("artists", "SELECT * FROM art_info WHERE artinf_Name = '$selname' ");
record_set("artgroups", "SELECT * FROM art_thumbs WHERE art_Name = '$selname' ");

$artistimageadded = $_GET[artist_added];

record_set("arcorder", "SELECT * FROM art_gallery WHERE artgal_Name = '$selname' ORDER BY artgal_Order DESC ");

record_set("groups", "SELECT * FROM art_gallery WHERE artgal_Name = '$selname' GROUP BY artgal_Group");

$added = "new images added for";
 $space = "&nbsp;";
 $duplicate = $_GET[test];
 $thumblink = $_POST[idlink];
//form insert


if ($_POST) {

if (!empty($_POST['idlink'])) {
	$artlink = $_POST['idlink'];
	record_set('thumblinkid',"SELECT * FROM art_thumbs WHERE art_IDlink = '".$artlink."' ");
}

//radio code
$selected_radio = $_POST['units'];

if ($selected_radio == 'cm') {
$cms_status = 'checked';
}
else if ($selected_radio == 'inches') {
$inches_status = 'checked';
}
//end radio code

//radio code framed
$fselected_radio = $_POST['funits'];

if ($fselected_radio == 'fcm') {
$fcms_status = 'checked';
}
else if ($fselected_radio == 'finches') {
$finches_status = 'checked';
}
//end radio code framed

//radio code groups
$selected_radio_grp = $_POST['grps'];

if ($selected_radio_grp == 'grp') {
$grps_status = 'checked';
}
//end radio code groups

//check to see if the 'title' already exists in the database
	$titlecheck = $_POST['art_title'];       
	$check = mysql_fetch_row(mysql_query("SELECT * FROM art_gallery WHERE artgal_Title = '".addslashes($titlecheck)."'"));         
	
	if ($check[0]) {
	//echo "Duplicate Title exists - Please use the Browser Back Arrow to go to the previous page";
		 header("Location: frm_artgallery.php?test=Duplicate-Title");
//$message = "&#40;Duplicate Title Exists&#41;";
//echo $message;
die;
	}
	//
		
	$formfile = "";
	      
	if($_FILES['art_Thumb'] && $_FILES['art_Thumb']['size'] > 0){
		$uploaddir = "images/artists/thumb/";  //folder in which to put the file
		$ori_name = $_FILES[art_Thumb][name];
		$tmp_name = $_FILES[art_Thumb][tmp_name];
		$src = imagecreatefromjpeg($tmp_name);
		list($width,$height) = getimagesize($tmp_name);
		//landscape
		if ($width >= $height) {
			$pwidth = 450;
			$newwidth = $pwidth;
			$newheight = ($height/$width)*$pwidth;
		}
		//
		//portrait
		if ($height >= $width) {
			$pwidth = $width;
			$newheight = 450;
			$newwidth = ($width/$height)*$newheight;
		}
		//
		$tmp = imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		$newfile = $uploaddir.$ori_name;
		imagejpeg($tmp,$newfile,100);
		chmod($newfile,0777);
	}
	
	$formfile1 = "";
	if($_FILES['art_Large'] && $_FILES['art_Large']['size'] > 0){
		$uploaddir1 = "images/artists/large/";  //folder in which to put the file
		$ori_name_large = $_FILES[art_Large][name];
		$tmp_name = $_FILES[art_Large][tmp_name];
		$src = imagecreatefromjpeg($tmp_name);
		list($width,$height) = getimagesize($tmp_name);
		//landscape
		if ($width >= $height) {
			$pwidth = 900;
			$newwidth = $pwidth;
			$newheight = ($height/$width)*$pwidth;
		}
		//
		//portrait
		if ($height >= $width) {
			$pwidth = $width;
			$newheight = 900;
			$newwidth = ($width/$height)*$newheight;
		}
		//
		$tmp = imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		$newfile_large = $uploaddir1.$ori_name_large;
		imagejpeg($tmp,$newfile_large,100);
		chmod($newfile_large,0777);
	}
	mysql_query("INSERT INTO art_gallery (artgal_LinkID, artgal_Name, artgal_Thumb, artgal_Large, artgal_Title, artgal_Com, artgal_com2, artgal_Med, artgal_sizeUnit, artgal_sp, artgal_sl, artgal_fsize, artgal_fsizep, artgal_fsizel, artgal_stat, artgal_Cost, artgal_Arc, artgal_Order, artgal_Group, artgal_GroupN) VALUES
		
		(
			'".$_POST[idlink]."',
            '".$_POST[art_Nme]."',
			'".$newfile."',
			'".$newfile_large."',
			'".$_POST[art_title]."',
			'".$_POST[art_com]."',
			'".$_POST[art_com2]."',
            '".$_POST[art_med]."',
			'".$selected_radio."',
			'".$_POST[im_size_port]."',
			'".$_POST[im_size_land]."',
            '".$fselected_radio."',
			'".$_POST[fim_size_port]."',
			'".$_POST[fim_size_land]."',
			'".$_POST[art_stat]."',
			'".$_POST[art_cost]."',
			'".$_POST[art_arc]."',
            '".$_POST[arc_orderS]."',
			'".$_POST[art_grp]."',
			'".$selected_radio_grp."'
			) 
		
	");
	
	//thumbnail gallery insert
	if ($_POST) {
	
			mysql_query("UPDATE art_thumbs SET					
			art_Group = '".$selected_radio_grp."'
			WHERE art_IDlink = ".$row_thumblinkid[art_IDlink]."			
				
	");
	}
		//end thumbnail gallery insert
	
				
	$redirect 
= 'frm_artgallery.php?'
. 'artist_added='
. urlencode($_POST['art_Nme'])
. '&'. 'image_added='
. urlencode($_POST['art_title'])
; 
	header('Location: '.$redirect); die;
	
	}
//end form insert	

?>

<!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=utf-8" />
<title>Lucy B Campbell Home Page</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />

<script src="jquery/jquery-1.8.3.min.js">
</script>

<script type="text/JavaScript">
jQuery(document).ready(function($) {
	$("#art_arc").change(function(){
		if($("#art_arc").val()=="Active"){
			$("#art_stat").val("");
		}
		if($("#art_arc").val()=="Archived"){
			$("#art_stat").val("Sold");
		}
	});	
});
</script>

<script language="javascript" type="text/javascript">
function select_group()
   {
   var x = document.frmGeneral.art_group_sel.selectedIndex;
   var selected_text = document.frmGeneral.art_group_sel.options[x].text;
	document.frmGeneral.art_grp.value=selected_text;
     }
</script>

<script type="text/JavaScript">
function selectOrder()
   {
   var x = document.frmGeneral.arc_order.selectedIndex;
   var selected_text = document.frmGeneral.arc_order.options[x].text;
	document.frmGeneral.arc_orderS.value=selected_text;
}
</script>
</head>

<body>
<?php require_once('incJScripts.php'); ?>
<?php require_once('incTinyMCE.html'); ?>
<div class="container" align="center">
  <div class="header_container"<?php require_once('incPageHeader.php'); ?></div>
<div class="body_container" >
<div align="left" class="body_text_large"><a href="index.php" target="_parent">Home Page</a>&nbsp;&#58;&#58;&nbsp;<span class="body_text_large_brickred">Artists Gallery Additions (Artist must be in the database in order to add an image)</span></div>
<br /><br />
<form action="frm_artgallery.php" method="GET">
  <div align="center"><span class="body_text_large">Select&nbsp;Artist&nbsp;Name (Required):&nbsp;</span>
      <select name="art_name" id="art_name" onchange='this.form.submit();'>
        <option value="value">Artist&nbsp;Name&#58;</option>
        <?php do { ?>
        <option <?php if ($_GET[art_name] == $row_artNames['artinf_Name']) { ?>selected="selected"<?php } ?> value="<?php echo urlencode($row_artNames['artinf_Name']); ?>"><?php echo $row_artNames['artinf_Name']?></option>
        <?php } while ($row_artNames = mysql_fetch_assoc($artNames)); ?>
      </select>
  </div>
</form>
<form action="frm_artgallery.php" enctype="multipart/form-data" name="frmGeneral" id="frmGeneral" method="post">

 <table width="675" border="0" align="center" cellpadding="0" class="">
  <tr>
    <td colspan="5" align="left" class="body_text_large"><a href="administration.php">Administration Page</a>:&nbsp;<span class="loggedin"><?php echo $_SESSION[loggedin]; ?></span></td>
    <tr>
    <td width="12" colspan="2" align="left">
    <a href="logout.php" target="_parent"><span class="loggedout">LOGOUT</span></a>    </td>
    </tr>
    <tr><td colspan="2"></tr>
  <tr>
    <td colspan="5"><hr width="200" size="1" /><br /><br /></td>
    </tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large">&nbsp;</td>
    <td class="body_text_large"><div align="right"><a href="artist_thumbnails.php?artist=<?php echo $artistimageadded; ?>">
    <?php if (!empty($artistimageadded )) {echo "Link to database images for current Artist";} ?></a><?php if (!empty($artistimageadded )) {echo $imad;} ?><?php if(!empty($artistimageadded )) { ?>
<img src="<?php echo $row_artgaladd[artgal_Thumb]; ?>" width="150"/> <?php } ?></div></td>
  </tr>
<tr>
  <td><br /><br /></td>
</tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large">&nbsp;</td>
    <td class="body_text_large" align="left">New Images will be added for Artist:&nbsp;<span class="body_text_message_artist"><?php echo $row_artists[artinf_Name]; ?><?php if(!empty($artistimageadded )) {
	                                echo $added.$space.$artistimageadded; } ?></span></td>
  </tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large" align="left"><label>Artist Name:&nbsp;</label></td>
    <td class="body_text_large" align="left"><input name="art_Nme" type="text" id="art_Nme" size="50" value="<?php echo $row_artists[artinf_Name]; ?>" readonly="readonly"/></td>
  </tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large" align="left">Thumbnail Image:&nbsp;</td>
    <td class="body_text_large" align="left"><input name="art_Thumb" type="file" id="art_Thumb" size="50" /></td>
  </tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large" align="left">Large Image:&nbsp;</td>
    <td class="body_text_large" align="left"><input name="art_Large" type="file" id="art_Large" size="50" /></td>
  </tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large" align="left">Image Title:&nbsp;</td>
    <td class="body_text_large" align="left"><input name="art_title" type="text" id="art_title" size="50" class="validate[required] text-input "/><span class="error_message"><?php echo $duplicate; ?></span></td>
  </tr>
  <tr>
    <td class="body_text_large" align="left"><label>Image Editions:&nbsp;</label></td>
    <td class="body_text_large" align="left"><input name="art_com" type="text" class="" id="art_com" size="50" /></td>
  </tr>
  <tr>
    <td nowrap="nowrap" class="body_text_large" align="left"><label>Image Com. 2:&nbsp;</label></td>
    <td class="body_text_large" align="left"><input name="art_com2 :" type="text" id="art_com2" size="50" /></td>
  </tr>
<tr>
    <td nowrap="nowrap" class="body_text_large" align="left"><label>Image Medium:&nbsp;</label></td>
    <td class="body_text_large" align="left"><input name="art_med" type="text" id="art_med" size="50" /></td>
  </tr>
  <tr>
    <td valign="top" class="body_text_large" align="left"><label>Image Size:</label></td>
    <td class="body_text_large" align="left"><label> Portrait:&nbsp;<input name="im_size_port" type="text" id="im_size_port" size="6" class="validate[required] text-input" />
    </label>
    &nbsp;&nbsp;&nbsp;<label> Landscape:&nbsp;<input name="im_size_land" type="text" id="im_size_land" size="6" class="validate[required] text-input" />
    &nbsp;
    <input type="radio" name="units" id="cms" value="cm" class="validate[condRequired[im_size_port]] text-input"  />
    cm
    </label>
    &nbsp;<input type="radio" name="units" id="inches" value="inches" class="validate[condRequired[im_size_land]] text-input"
<?php if (!empty($row_artgaladd[artgal_sizeUnit])) { ?>
	checked="checked" <?php } ?> />
    inches
    &nbsp;<span class="body_text">(decimals please)</span></td>
  </tr>
 <tr>
    <td valign="top" class="body_text_large" align="left"><label>Framed Size:</label></td>
    <td class="body_text_large" align="left"><label> Portrait:&nbsp;<input name="fim_size_port" type="text" id="fim_size_port" size="6" class="" />
    </label>
    &nbsp;&nbsp;&nbsp;<label> Landscape:&nbsp;<input name="fim_size_land" type="text" id="fim_size_land" size="6" class="" />
    &nbsp;
    <input type="radio" name="funits" id="fcms" value="cm" class=""  />
    cm
    </label>
    &nbsp;<input type="radio" name="funits" id="finches" value="inches" class=""
<?php if (!empty($row_artgaladd[artgal_fsize])) { ?>
	checked="checked" <?php } ?> />
    inches
    &nbsp;<span class="body_text">(decimals please)</span></td>
  </tr>
  <tr>
    <td valign="top" class="body_text_large" align="left"><label>Status:&nbsp;</label></td>
    <td class="body_text_large" align="left"><label>
      <select name="art_stat" id="art_stat">
              <option value=""></option>
        <option value="Sold" <?php if ($row_artgaladd[artgal_stat] == 'Sold') echo ' selected="selected"'; ?>>Sold</option>
      </select>
    </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>Image&nbsp;Status:</label><label>
      <select name="art_arc" id="art_arc">
         <option value=""></option>        
<option value="Active" <?php if ($row_artgaladd[artgal_Arc] == 'Active') echo ' selected="selected"'; ?>>Active</option>
<option value="Archived" <?php if ($row_artgaladd[artgal_Arc] == 'Archived') echo ' selected="selected"'; ?>>Archived</option>
<option value="Deleted" <?php if ($row_artgaladd[artgal_Arc] == 'Deleted') echo ' selected="selected"'; ?>>Deleted</option>
      </select>
    </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>Archive&nbsp;Order:</label>

<input name="arc_orderS" id="arc_orderS"  type="text" size="1" value="<?php echo $row_artists[artgal_Order]; ?>" />
<select name="arc_order" id="arc_order" onChange="selectOrder()">
        <option value=""></option>
        <?php do { ?>
        <option <?php if ($row_arcorder[artgal_Order] == $row_arcorder['artgal_Order']) { ?>selected="selected"<?php } ?> value="<?php echo $row_arcorder['artgal_Order']; ?>"><?php echo $row_arcorder['artgal_Order']; ?></option>
        <?php } while ($row_arcorder = mysql_fetch_assoc($arcorder)); ?>
      </select>

</td>
  </tr>
<tr>
    <td valign="top" class="body_text_large" align="left"><label>Groups:</label></td>
<td class="body_text_large" align="left"><input name="art_grp" type="text" id="art_grp" size="15" />&nbsp;
<select name="art_group_sel" id="art_group_sel" onchange="select_group()" >
        <option value=""></option>
        <?php do { ?>
        <option <?php if ($row_groups['artgal_Group'] == $row_groups['artgal_Group']) { ?>selected="selected"<?php } ?> value="<?php echo $row_groups['artgal_Group']; ?>"><?php echo $row_groups['artgal_Group']?></option>
        <?php } while ($row_groups = mysql_fetch_assoc($groups)); ?>
      </select>
&nbsp;<input type="radio" name="grps" id="grp" value="1" class=""
<?php if (!empty($row_artgroups[art_Group])) { ?>
	checked="checked" <?php } ?>

 />&nbsp;Activated (If activated, select a group)</td>
</tr>
  <tr>
    <td valign="top" class="body_text_large" align="left">Cost:</td>
    <td class="body_text_large" align="left"><input name="art_cost" type="text" id="art_cost" size="10" /></td>
  </tr>
  <tr>
    <td colspan="2" align="right" class="body_text_large"><label>
    <input type="hidden" name="idlink" id="idlink" value="<?php echo $row_artists[link_ID] ?>" />
      <input type="submit" name="button" id="button" value="Submit" />
    </label></td>
  </tr>
</table>

</form><br />
</div><br /><br /><br /><br />

<div class="body_hr"><hr size="1">
<div class="body_text" align="left"><?php require_once('incPageFooter.php'); ?></div>
</div>

</body>
</html>

Open in new window

0
Comment
Question by:doctorbill
  • 3
  • 3
6 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
Comment Utility
If you expect a permissions issue, you may be able to debug this by examining the directories involved.  A teaching example is shown below in the code snippet.  You may be able to install this "as-is" and use it to examine your directory and file permissions.  This can be done in isolation, but you might also want to add error_reporting(E_ALL) to the top of your PHP script and correct anything that PHP notifies you about.

The general design for an application like this would be as follows:

1. Upload the file, then use PHP move_uploaded_file() to place the file into a safe storage location.  You can't use the system's temporary storage safely.  Explanations are given in the  link.

2. Using the safely moved file, do any operations you want, such as resizing and storing into a permanent location.

3. (optional) Unlink() the working copy you made in step 1.

4. Perform any database updates.

You can use something like this code to examine the files, file paths and directories.  I've tested it on Linux.  If you're using Windows, YMMV, so test carefully.
<?php // demo/temp_doctorbill.php

/**
 * SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28603137.html
 *
 * FROM THE OP AT E-E:
 *
 * $uploaddir = "images/artists/thumb/";  //folder in which to put the file
 *
 * MAN PAGE REFERENCES:
 * http://php.net/manual/en/function.clearstatcache.php
 * http://php.net/manual/en/function.is-dir.php
 * http://php.net/manual/en/function.is-writable.php
 * http://php.net/manual/en/function.fileperms.php
 *
 */
error_reporting(E_ALL);

// A PATH TO A KNOWN FOLDER ON MY SERVER
$uploaddir
= 'polyglot'
. DIRECTORY_SEPARATOR
. 'image'
. DIRECTORY_SEPARATOR
;

// MAKE SURE WE ARE WORKING WITH GOOD FILE INFORMATION
clearstatcache();


// IS THIS A DIRECTORY?
if (is_dir($uploaddir)) echo PHP_EOL . "$uploaddir IS A DIRECTORY<br>";

// IS THIS DIRECTORY WRITABLE?
if (is_writable($uploaddir)) echo PHP_EOL . "$uploaddir IS WRITEABLE<br>";


// WHAT IS THE COMPLETE PATH?
$path = getcwd() . DIRECTORY_SEPARATOR . $uploaddir;
echo PHP_EOL . "$path IS THE COMPLETE PATH<br>";
if (is_writable($path)) echo PHP_EOL . "$path IS WRITEABLE<br>";


// WHAT ARE THE FILE PERMISSIONS?
echo "$path PERMISSIONS: " . format_file_perms($path);


// A FUNCTION TO FORMAT THE FILE PERMISSIONS INTO AN EASY-TO-READ STRING
function format_file_perms($path, $nums=TRUE)
{
    $perms = fileperms($path);

    // FILE OR PATH TYPE
    if (($perms & 0xC000) == 0xC000)     $info = 's'; // SOCKET
    elseif (($perms & 0xA000) == 0xA000) $info = 'l'; // SYMLINK
    elseif (($perms & 0x8000) == 0x8000) $info = '-'; // REGULAR
    elseif (($perms & 0x6000) == 0x6000) $info = 'b'; // BLOCK
    elseif (($perms & 0x4000) == 0x4000) $info = 'd'; // DIRECTORY
    elseif (($perms & 0x2000) == 0x2000) $info = 'c'; // CHARACTER
    elseif (($perms & 0x1000) == 0x1000) $info = 'p'; // PIPE
    else                                 $info = 'u'; // UNKNOWN

    // OWNER
    $info .= ' ';
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ?
             (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));

    // GROUP
    $info .= ' ';
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ?
             (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));

    // WORLD
    $info .= ' ';
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ?
             (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));

    if ($nums) $info .= ' (' . substr(sprintf('%o', $perms), -4) . ')';

    return $info;
}

Open in new window

0
 

Author Comment

by:doctorbill
Comment Utility
Hi Ray
Thanks for the info

I have carried out some tests and if I make the initial file I am uploading  much smaller (< 1 mb) it then works with the script I posted
Is there some limiting factor in the script / apache server ?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
That's good information to have!  You have to tell PHP to allow larger uploads.  Here is my teaching example showing how to upload a file (image sizing and database work are separate topics, not covered here).  Check the man page links to find all the important points.  You may need to update php.ini for some of the settings.  You can find the current values on your server by running phpinfo().  Also, follow the variable $max_file_size through this script to see how it is used.

<?php // demo/upload_single_example.php

/**
 * Demonstrate how to upload a single file in PHP
 *
 * REQUIRED: Man Page References
 * http://php.net/manual/en/reserved.variables.files.php
 * http://php.net/manual/en/features.file-upload.php
 * http://php.net/manual/en/features.file-upload.common-pitfalls.php
 * http://php.net/manual/en/function.move-uploaded-file.php
 *
 * IMPORTANT: If dealing with large files
 * http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
 * http://php.net/manual/en/ini.core.php#ini.post-max-size
 * http://php.net/manual/en/info.configuration.php#ini.max-input-time
 */
error_reporting(E_ALL);

// PHP 5.1+  SEE http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT - ABOUT 8 MB
$max_file_size = '8000000';

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE WANT (USE UPPERCASE ONLY)
$file_exts = array
( 'XLS'
, 'XLSX'
, 'PDF'
, 'JPG'
)
;
$f_exts = implode(', ', $file_exts);

// ESTABLISH THE NAME OF THE DESTINATION FOLDER
$my_dir = getcwd();

// OR USE THIS TO PUT UPLOADS IN A SEPARATE FOLDER
$my_dir = 'storage';
if (!is_dir($my_dir))
{
    mkdir($my_dir);
}

// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;


// IF THERE IS INFORMATION POSTED
if (!empty($_POST))
{
    // IF THERE ARE ERRORS
    $error_code    = $_FILES["userfile"]["error"];
    if ($error_code)
    {
        trigger_error($errors[$error_code], E_USER_ERROR);
    }

    // GET THE FILE SIZE
    $fsize = number_format($_FILES["userfile"]["size"]);

    // SYNTHESIZE THE NEW FILE NAME FOR TEMPORARY STORAGE
    $fname = basename($_FILES['userfile']['name']);

    // FAIL IF THIS IS NOT AN ALLOWABLE EXTENSION
    $f_ext = explode('.', $fname);
    $f_ext = end($f_ext);
    $f_ext = strtoupper(trim($f_ext));
    if (!in_array($f_ext, $file_exts)) trigger_error("$f_ext NOT ALLOWED.  CHOOSE FROM $f_exts", E_USER_ERROR);

    // THE SERVER PATH TO THE FILE
    $my_path
    = getcwd()
    . DIRECTORY_SEPARATOR
    . $my_dir
    . DIRECTORY_SEPARATOR
    . $fname
    ;

    // THE URL PATH TO THE FILE
    $my_url
    = $my_dir
    . DIRECTORY_SEPARATOR
    . $fname
    ;

    // MESSAGES ABOUT THE UPLOAD STATUS, IF ANY
    $msg = NULL;

    // IF THE FILE IS NEW (DOES NOT EXIST)
    if (!file_exists($my_path))
    {
        // IF THE MOVE FUNCTION WORKED CORRECTLY
        if (move_uploaded_file($_FILES['userfile']['tmp_name'], $my_path))
        {
            $upload_success = 1;
        }
        // IF THE MOVE FUNCTION FAILED IT PROBABLY THREW A MESSAGE
        else
        {
            $upload_success = -1;
            trigger_error("MOVE TO $my_path FAILED", E_USER_ERROR);
        }
    }

    // IF THE FILE ALREADY EXISTS
    else
    {
        $msg .= "<br/><b><i>$my_url</i></b> already exists" . PHP_EOL;

        // SHOULD WE OVERWRITE THE FILE? IF NOT
        if (empty($_POST["overwrite"]))
        {
            $upload_success = 0;
        }

        // IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
        else
        {
            $now    = date('Y-m-d-His');
            $my_bak = $my_path . '.' . $now . '.bak';
            if (!copy($my_path, $my_bak))
            {
                $msg .= "<br/><strong>Attempted Backup Failed!</strong>" . PHP_EOL;
            }
            if (move_uploaded_file($_FILES['userfile']['tmp_name'], $my_path))
            {
                $upload_success = 2;
            }
            else
            {
                $upload_success = -1;
                trigger_error("MOVE TO $my_path FAILED", E_USER_ERROR);
            }
        }
    }

    // PREPARE A REPORT OF THE SCRIPT'S SUCCESS OR FAILURE
    if ($upload_success == 2) { $msg .= "<br/>A backup was made and the file was overwritten" . PHP_EOL; }
    if ($upload_success == 1) { $msg .= "<br/><strong>$my_url</strong> has been saved" . PHP_EOL; }
    if ($upload_success == 0) { $msg .= "<br/><strong>It was NOT overwritten.</strong>" . PHP_EOL; }
    if ($upload_success < 0)  { $msg .= "<br/><strong>ERROR: $my_url NOT SAVED - SEE WARNING FROM move_uploaded_file()</strong>" . PHP_EOL; }

    // ADD FILE SIZE AND PERMISSION INFORMATION
    if ($upload_success > 0)
    {
        $msg .= "<br/>$fsize bytes uploaded" . PHP_EOL;
        if (!chmod ($my_path, 0755))
        {
            $msg .= "<br/>chmod(0755) FAILED: fileperms() = ";
            $msg .= substr(sprintf('%o', fileperms($my_path)), -4);
        }
    }

    // SHOW THE SUCCESS OR FAILURE
    echo $msg;

    // SHOW A LINK TO THE FILE
    echo '<br/>'
    . '<a href="'
    . $my_url
    . '" target="_blank">'
    . "See: $my_url"
    . '</a>'
    ;
}


// CREATE THE FORM FOR INPUT (USING HEREDOC SYNTAX)
$form = <<<EOF
<h2>Upload a File</h2>
<p>
<form enctype="multipart/form-data" method="post">
<!--
    SOME IMPORTANT THINGS TO NOTE ABOUT THIS FORM...
    ENCTYPE= ATTRIBUTE IN THE HTML <FORM> TAG
    MAX_FILE_SIZE HIDDEN CONTROL MUST PRECEDE THE FILE INPUT CONTROLS
    INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN _FILES ARRAY
    ABSENCE OF ACTION= ATTRIBUTE IN <FORM> TAG CAUSES POST TO SAME URL
-->
<input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size" />
Find a file to Upload ($f_exts): <input name="userfile" type="file" />
<br/>Check this box
<input autocomplete="off" type="checkbox" name="overwrite" /> to <strong>overwrite</strong> existing files
</br><input type="submit" value="Upload" />
</form>
</p>
EOF;

echo $form;

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:doctorbill
Comment Utility
As usual
Perfect example and 100%
Thanks a million
0
 

Author Closing Comment

by:doctorbill
Comment Utility
Solution
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points -- it's a great question! ~Ray
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now