Jump to content

[SOLVED] Updating a session variable


mediabob

Recommended Posts

Hi, I am trying to create an upload for so that visitors to my site can upload files, I am doing th form in 2 stages, the first stage the user will upload the file, then in the second stage the user will fill out a form with information about the file. Now, here is what I am trying to do, I have a session variable name $ufile and when a user uploads the file I would like to update this variable with the name of the file they uploaded so that it can carry over to the form and they will not have to manually fill in a URL for the download location.

 

I have the upload form working fine, but I can not seem to update the $ufile variable in the upload form, depending on where I try to update it the variable either clears to nothing or just does not update (I give the variable a value on creation for testing). Here is my download script, this is everything before the <head> tag that processes the form. Exactly where and how would I update the $ufile seesion variable to be the same as $file_name:

 

<?php
include('auth.php');
$MAX_SIZE = 8388608;
$FILE_EXTS  = array('.gmd','.gm6','.zip','.rar','.gmk'); 
$DELETABLE  = false;                               


$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);	
$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];			

$upload_dir = "efiles/";				
$upload_url = $url_dir."efiles/";		
$message ="message";						
if (!is_dir("efiles")) {
  if (!mkdir($upload_dir))
  	die ("public files directory doesn't exist and creation failed");
  if (!chmod($upload_dir,0755))
  	die ("change permission to 755 failed.");
}


if ($_REQUEST[del] && $DELETABLE)  {
  $resource = fopen("log.txt","a");
  fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
  fclose($resource);
  
  if (strpos($_REQUEST[del],"/.")>0);
  else if (strpos($_REQUEST[del],$upload_dir) === false);
  else if (substr($_REQUEST[del],0,6)==$upload_dir) {
    unlink($_REQUEST[del]);
    print "<script>window.location.href='$url_this?message=deleted successfully'</script>";
  }
}
else if ($_FILES['userfile']) {
  $resource = fopen("log.txt","a");
  fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
            .$_FILES['userfile']['name']." "
            .$_FILES['userfile']['type']."\n");
  fclose($resource);

  $file_type = $_FILES['userfile']['type']; 
  $file_name = $_FILES['userfile']['name'];
  $file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
  
   
  //File Size Check
  if ( $_FILES['userfile']['size'] > $MAX_SIZE) 
     $message = "The file size exceeds the limit of 8MB.";		
  //File Extension Check
  else if (!in_array($file_ext, $FILE_EXTS))
     $message = "Sorry, $file_name($file_type) is not allowed to be uploaded. We only allow certain filetypes to be uploaded to the public folder.";
  else
     $message = do_upload($upload_dir, $upload_url);
  
  print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else 
$message = "Invalid File Specified.";


$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
   if(!is_dir($file) && !is_link($file)) {
      $filelist .= "<a href='$upload_dir$file'>".$file."</a> - URL: <b>$upload_url$file</b><br />";
      if ($DELETABLE)
        
      $filelist .= " Added at  ".date("d-m H:i", filemtime($upload_dir.$file))
                   ."";
   }
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_FILES['userfile']['name']; 
    $file_name = str_replace("\\","",$file_name);
    $file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;

   

//File Name Check
  if ( $file_name =="") { 
  	$message = "Invalid File Name Specified";
  	return $message;
  }
  $result  =  move_uploaded_file($temp_name, $file_path);
  if (!chmod($file_path,0755))
   	$message = "change permission to 755 failed. Please manually chmod your public files folder to 755.";
  else
    $message = ($result)?"$file_name was uploaded successfully." :
     	      "An error occurred while uploading the file.";		  
   return $message;
}

?>

 

*Note the session is started in auth.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.