Jump to content

File upload not working


Merdok

Recommended Posts

Hi Guys,

 

I found this upload script a while ago and I'm sure the last time I used it, it worked perfectly but for some reason '$path1' keeps returning as blank. I'm not very good with file uploads, can anyone help me out please?

 

if ($_POST['customBackground']) {		
			$path1= '/upload/' . date(U). $HTTP_POST_FILES['customBackground']['name'][0];
			if(empty($path1)) { $message = '<p class="red size10font">Background field cannot be blank!</p>';} else {
				//copy file to where you want to store file
				copy($HTTP_POST_FILES['customBackground']['tmp_name'][0], $path1);
				}
}

 

The field it is being populated from is

 

<input class="fullwidth biggun" name="customBackground" type="file" id="customBackground" size="70" value="">

 

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/232701-file-upload-not-working/
Share on other sites

$HTTP_POST_FILES has been deprecated for a long time, so your php version probably doesn't include it anymore. Try using $_FILES:

if (isset($_POST['customBackground']) && $_FILES['customBackground']['error'] == UPLOAD_ERR_OK) {		
			$path1= '/upload/' . date('U'). $_FILES['customBackground']['name'];
			if(empty($path1)) { $message = '<p class="red size10font">Background field cannot be blank!</p>';} else {
				//copy file to where you want to store file
				copy($_FILES['customBackground']['tmp_name'], $path1);
				}
}

Add back the [0] part if it's only supposed to use the first character of the filename.

Thank you for the advice, I had no idea it was outdated :) Sadly it did not work though, here is the code for the entire page just in case the problem is located elsewhere:

 

<?php 

//tells the menu which module this is
$current_module = 2;

require_once('../../templates/standard/socket_header.php'); ?>
                    <p class="float_right button"> <a href="<?php echo $siteroot?>/socket/index.php">Discard</a></p><h1>Article Editor</h1>
          <p>From here you can edit your blog articles.</p>
          <?php
if($_GET['ID'])
{
// Pulls the data from the database
$dblookup = "SELECT * FROM core_pages WHERE articleID=" . $_GET['ID'];
$data = mysql_query($dblookup) or die('Failed to return data: ' . mysql_error());
/* sorts the data into variables and puts them in an array ready to be called when needed */
$dataarray = mysql_fetch_array($data, MYSQL_BOTH);
extract($dataarray, EXTR_PREFIX_ALL, "bl");
}
elseif (!empty($_POST['submit'])) // If the updates have been submitted
{
	// Save previous article to the revisions table
	$saveArticle = mysql_query("SELECT * FROM core_pages WHERE articleID =" . $_POST['articleID']) or die ('No articles found');
	$savedArticles = mysql_fetch_array($saveArticle, MYSQL_BOTH);
	extract($savedArticles, EXTR_PREFIX_ALL, "saved");
	$currentUser = $_SESSION['userID'];
	$dbinsert = mysql_query("INSERT INTO core_pages_revisions (articleID, articleTitle,articleKeywords,articleDesc, permaLink, articleBody, allowCustomFooter, customFooter, chooseBackground, customBackground, articlePosted, parent) VALUES ('$saved_articleID', '".addslashes($saved_articleTitle)."', '".addslashes($saved_articleKeywords)."', '".addslashes($saved_articleDesc)."', '".addslashes($saved_permaLink)."', '".addslashes($saved_articleBody)."', '".addslashes($saved_allowCustomFooter)."','".addslashes($saved_customFooter)."','".addslashes($saved_chooseBackground)."','".addslashes($saved_customBackground)."', '".addslashes($saved_articlePosted)."', '".addslashes($saved_parent)."')") or die ('insertion failed: ' . mysql_error());
	// Now insert the updates into the live database
		$articleID 	 		= $_POST['articleID'];
		$parent		 		= $_POST['parent'];
		$articleTitleDirty	= str_replace("...","",$_POST['articleTitle']);
		$articleDescDirty	= $_POST['articleDesc'];
		$articleKeywordsDirty= $_POST['articleKeywords'];
		$articleTitle 		= htmlentities(addslashes($articleTitleDirty));
		$articleDesc 		= htmlentities(addslashes($articleDescDirty));
		$articleKeywords	= htmlentities(addslashes($articleKeywordsDirty));
		$permaLinkDirty		= str_replace(" ","-",$_POST['articleTitle']);
		$permaLinkDashed	= strtolower(preg_replace('/[^A-Za-z0-9-]/','',$permaLinkDirty));
		$permaLink 			= str_replace("---","-",$permaLinkDashed);
		$articleBody 		= addslashes($_POST['articleBody']);
		$allowCustomFooter	= addslashes($_POST['allowCustomFooter']);
		$customFooter		= addslashes($_POST['customFooter']);
		$chooseBackground	= addslashes($_POST['chooseBackground']);
		// Handles file uploading
		if (isset($_POST['customBackground']) && $_FILES['customBackground']['error'] == UPLOAD_ERR_OK) {
			$path1= '/upload/' . date('U'). $_FILES['customBackground']['name'];
			if(empty($path1)) { $message = '<p class="red size10font">Background field cannot be blank!</p>';} else {
				//copy file to where you want to store file
				copy($_FILES['customBackground']['tmp_name'], $path1);
			}
		}
		if ($_POST['submit'] == 'Publish Page' || $saved_articlePosted ==1) {$articlePosted = 1;}
		// Updates the database
		$dbupdate  = "UPDATE core_pages ";
        $dbupdate .= "SET articleTitle = '$articleTitle', articleDesc = '$articleDesc', articleKeywords = '$articleKeywords', permaLink = '$permaLink', articleBody = '$articleBody',";
		if ($allowCustomFooter == 1) {  $dbupdate .= " allowCustomFooter = '$allowCustomFooter', customFooter = '$customFooter',";  }
		if ($chooseBackground == 1) {  $dbupdate .= " chooseBackground = '$chooseBackground', customBackground = '$path1',";  }
		$dbupdate .= " parent = '$parent', articlePosted = '$articlePosted'";
		$dbupdate .= " WHERE articleID = '$articleID'";
		$posted = mysql_query($dbupdate) or die('<h3 class="error"> Update Failed! </h3>' . mysql_error());
if ($posted) 
{
$message = '<div class="success"><p><strong>Success!</strong> Your article has been modifed</p></div>';
?>
<!-- javascript send message to menu -->
<script language="JavaScript">
      window.location.href = '<?php echo $socketroot ?>/modules/pages/admin.php?message=' + <?php echo $message; ?>;
</script>
	<?php }}
	?>
          <h2> <?php echo "Curently Editing: " . html_entity_decode(stripslashes($bl_articleTitle));?></h2>
		<form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
            <input name="articleID" type="hidden" id="articleID" value="<?php echo $bl_articleID; ?>">
            <p>
              <label>Full Title<br />
                <input name="articleTitle" type="text" id="articleTitle" size="70" value="<?php echo html_entity_decode(stripslashes($bl_articleTitle)) ?>">
              </label>
            </p>
            <p>
              <label>Body Text<br />
                <textarea class="mceAdvanced" name="articleBody" id="articleBody" cols="75" rows="35"><?php echo stripslashes($bl_articleBody) ?></textarea>
              </label>
            </p>
           <p>
              <label>Keywords (comma seperated)<br />
                <textarea class="noMCE" name="articleKeywords" id="articleKeywords" cols="81" rows="8"><?php echo stripslashes($bl_articleKeywords) ?></textarea>
              </label>
            </p>
            <p>
              <label>Description<br />
                <textarea class="noMCE" name="articleDesc" id="articleDesc" cols="81" rows="8"><?php echo stripslashes($bl_articleDesc) ?></textarea>
              </label>
            </p>
           <div class="inputcontainer">
           <label class="tab" for="articleCat">Select Parent <select class="intab" name="parent">
	  <?php 
          $lookupParents = "SELECT permaLink, articleTitle FROM core_pages WHERE parent = 0";
	  $parents = mysql_query($lookupParents) or die('Failed to return data: ' . mysql_error());
		/* sorts the data into variables and puts them in an array ready to be called when needed */
	  echo '<option value="'.$bl_parent.'" selected="selected"> Don\'t change parent </option>';
	  if ($bl_parent != 0) { echo '<option value="0"> Promote page to parent </option>'; }
          while($option = mysql_fetch_array($parents)) {
          echo '<option value="' . $option['permaLink'];
	  echo '">' .$option['articleTitle'].'</option>';
          }
          ?>
   </select></label>
            </div>	
             <div class="inputcontainer">
              <label class="tab" for="allowCustomFooter">Footer Options
			<select onchange="if( $(this).val() == 1 ) { $('.customFooterContainer').show() } else { $('.customFooterContainer').hide() }" name="allowCustomFooter" id="allowCustomFooter">
                	<option value="0">Default Footer</option>
                	<option value="1">Use custom Footer</option>
                 </select>
              </label>
            </div>
            <div class="inputcontainer customFooterContainer">
            	<textarea class="mceAdvanced" name="customFooter" id="customFooter" cols="75" rows="35"></textarea>
            </div>
            <div class="inputcontainer">
              <label class="tab" for="chooseBackground">Background Options
			<select onchange="if( $(this).val() == 1 ) { $('.customBackgroundContainer').show() } else { $('.customBackgroundContainer').hide() }" name="chooseBackground" id="chooseBackground">
                	<option value="0">Default Background</option>
                	<option value="1">Use custom Background</option>
                 </select>
              </label>
            </div>
            <div class="inputcontainer customBackgroundContainer">
            	<label class="tab" for="articleTitle">Choose custom background</label>
                <input class="fullwidth biggun" name="customBackground" type="file" id="customBackground" size="70" value="">
            </div>
            <?php if ($bl_articlePosted != 1) { ?> 
            <input name="submit" type="submit" value="Publish Page">
            <?php } ?>
		<input name="submit" type="submit" value="Save Changes">

          </form>
          <?php require_once('../../templates/standard/socket_footer.php'); ?>

Hmm.. looks like $_POST doesn't get set with any kind of value of the upload <input> so do this instead:

			if (isset($_FILES['customBackground']) && $_FILES['customBackground']['error'] == UPLOAD_ERR_OK) {
			$path1= '/upload/' . date('U'). $_FILES['customBackground']['name'];
			if(empty($path1)) { $message = '<p class="red size10font">Background field cannot be blank!</p>';} else {
				//copy file to where you want to store file
				copy($_FILES['customBackground']['tmp_name'], $path1);
			}
		}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.