SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    Non-Member
    Join Date
    Apr 2007
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Want to add re-direct when Submit is selected

    Hi,

    I'm using this Upload script and would simply like to re-direct the User, after he's Uploaded, to a different location. I don't know where/how to do it?

    Can you help me?
    Someone suggested:
    At the very bottom of this file, right after this line: return $message;
    Add this: header("Location: http://whereveryouwantthemtogo.com");

    Which I did, but I wasn't redirected. Any thoughts/suggestions/assistance will be greatly appreciated.

    Code:
    <?
    		// Begin options
    
    	$allow_file_deletion = true; // To allow visitors to delete files, leave this at true; otherwise, change it to false
    
    	$file_extensions = array(".doc", ".rtf", ".htm", ".html", ".pdf", ".txt"); // Add or delete the file extensions you want to allow
    
    	$file_extensions_list = "doc,  rtf,  htm,  html,  pdf,  txt"; // Type the same as above, without the quotes separating them
    
    	$max_length = 50; // The maximum character length for a file name
    
    	$maximum_file_size = "2048000"; // In bytes
    
    	$upload_log_file = "upload_log.txt"; // Change this to the log file you want to use
    
    	// End options
    
    	$folder_directory = "http://".$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]);
    	$message = "";
    	$set_chmod = 0;
    	$site_uri = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];
    	$upload_directory = "files/";
    	$upload_uri = $folder_directory."/files/";
    
    	if($allow_file_deletion == true) $status = "enabled";
    	else $status = "disabled";
    
    	if($_REQUEST["delete"] && $allow_file_deletion) {
    	$resource = fopen($upload_log_file,"a");
    	fwrite($resource,date("F d, Y / h:i:sa")." - ".$_REQUEST["delete"]." deleted by ".$_SERVER["REMOTE_ADDR"]."\n");
    	fclose($resource);
    
    	if(strpos($_REQUEST["delete"],"/.") > 0);
    	elseif(strpos($_REQUEST["delete"],$upload_directory) === false);
    	elseif(substr($_REQUEST["delete"],0,6) == $upload_directory) {
    	unlink($_REQUEST["delete"]);
    	$message = "File has been deleted.";
    	header("Location: $site_uri?message=$message");
    	}
    	}
    
    	elseif($_FILES["userfile"]) {
    	$resource = fopen($upload_log_file,"a");
    	
    fwrite($resource,date("F d, Y / h:i:sa")." - ".$_FILES["userfile"] ["name"]." ".$_FILES["userfile"]["type"]." (".$_FILES["userfile"]["size"]." bytes) uploaded by ".$_SERVER["REMOTE_ADDR"]."\n");
    	fclose($resource);
    
    
    	$file_type = $_FILES["userfile"]["type"]; 
    	$file_name = $_FILES["userfile"]["name"];
    	$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
    	@chmod($upload_uri."".$file_name, 0755);
    	if($_FILES["userfile"]["size"] > $maximum_file_size) {
    	$message = "ERROR: File size cannot be over ".$maximum_file_size." bytes.";
    	}
    
    	elseif($file_name == "") $message = "ERROR: Please select a file to upload.";
    	elseif(strlen($file_name > $max_length)) $message = "ERROR: The maximum length for a file name is ".$max_length." characters.";
    	elseif(!preg_match("/^[A-Z0-9_.\- ]+$/i",$file_name)) $message = "ERROR: Your file name contains invalid characters.";
    	elseif(!in_array($file_ext, $file_extensions))
    	$message = "ERROR: <ins>$file_ext</ins> is not an allowed file extension.";
    	else $message = upload_file($upload_directory, $upload_uri);
    	header("Location: $site_uri?message=$message");
    	}
    
    	elseif(!$_FILES["userfile"]);
    	else $message = "ERROR: Invalid file specified.";
    
    	$open = opendir($upload_directory);
    	$uploaded_files = "";
    	while($file = readdir($open)) {
    	if(!is_dir($file) && !is_link($file)) {
    	$uploaded_files .= "		<tr>
    				<td style=\" font-size:12px;color:#900;text-align: left; width: 70%\"> <a href=\"$upload_directory$file\" title=\"$file (".filesize($upload_directory."".$file)." bytes)\">".$file."</a> (".filesize($upload_directory."".$file)." bytes)</td>";
    	if($allow_file_deletion)
    	$uploaded_files .= "
    				<td style=\"background: #fff; font-size:12px;color:#6B6B6B; text-align: right; width: 30%\"><a href=\"?delete=$upload_directory".urlencode($file)."\" title=\"Delete File\">Delete File</a></td>";
    	else
    	$uploaded_files .= "
    				<td style=\"background: #fff; font-size:12px;color:#6B6B6B; text-align: right; width: 30%\"><del>Delete File</del></td>";
    	$uploaded_files .= "
    			</tr>
    			<tr>
    				<td colspan=\"2\" style=\"background: #eee; font-size:12px;color:#6B6B6B; text-align: left; text-indent: 20px\">        Uploaded--"     .date("F d, Y ", filemtime($upload_directory.$file))."</td>";
    	$uploaded_files .="
    			</tr>
    	";
    	}
    	}
            function upload_file($upload_directory, $upload_uri) {global $account;
            $file_name = $_FILES["userfile"]["name"];
    	$file_name = str_replace(" ","_",$file_name);
    	$ext=pathinfo($file_name,PATHINFO_EXTENSION);
            $file_name = basename($file_name, '.' . $ext);
            $file_path = $upload_directory. $file_name.'~~'. $account->get_user_name().'.'.$ext;
    
    	$temporary = $_FILES["userfile"]["tmp_name"];
    
    	$result = move_uploaded_file($temporary, $file_path);
    	if(!chmod($file_path,0777))
    	$message = "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
    	else $message = ($result)?"File has been uploaded." : "An error has occurred.";
    	return $message;
    header("Location: http://www.adifferentwebpage.com");
    	}
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take that line out of the upload_file() once it hits the line that has return on it it exits the function. Place your header line in your code after the function executes.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    Non-Member
    Join Date
    Apr 2007
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you for your reply.

    Thanks for your reply. It sounds like just what I need,
    however, I can't understand what you're instructing me to do.

    Could you please clarify your reply, please?

    thank you

  4. #4
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    # this is your upload_file() function
    function upload_file($upload_directory$upload_uri) {global $account;
            
    $file_name $_FILES["userfile"]["name"];
        
    $file_name str_replace(" ","_",$file_name);
        
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);
            
    $file_name basename($file_name'.' $ext);
            
    $file_path $upload_directory$file_name.'~~'$account->get_user_name().'.'.$ext;

        
    $temporary $_FILES["userfile"]["tmp_name"];

        
    $result move_uploaded_file($temporary$file_path);
        if(!
    chmod($file_path,0777))
        
    $message "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";
        else 
    $message = ($result)?"File has been uploaded." "An error has occurred.";
        return 
    $message;

    # you currently have this line within your function
    header("Location: http://www.adifferentwebpage.com");
    #you need to take it out of the function and place it AFTER the function
        
    // end of function here
    # place header here: header("Location: http://www.adifferentwebpage.com");

    ?>
    Regards,
    BJ Duncan

  5. #5
    Non-Member
    Join Date
    Apr 2007
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks

    Thanks for your reply.

    Your suggestion looked promising.

    I moved the header outside of the function, at the bottom, right before: ?>

    Now, though instead of me accessing the Upload page, like before, when I select the upload page link, I go right to the designated header location.

    It skips the Upload page.

    My initial intention here is to reach the Upload Page, upload and then be re-directed.

    Any other thoughts, or suggestions?

    Thanks again.

  6. #6
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about if you add the re-direct after an if statement:
    PHP Code:
    <?php
    # this is your upload_file() function 
    function upload_file($upload_directory$upload_uri) {
    global 
    $account;         
    $file_name $_FILES["userfile"]["name"];     
    $file_name str_replace(" ","_",$file_name);     
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);         
    $file_name basename($file_name'.' $ext);         
    $file_path $upload_directory$file_name.'~~'$account->get_user_name().'.'.$ext;      
    $temporary $_FILES["userfile"]["tmp_name"];      
    $result move_uploaded_file($temporary$file_path);     
    if(!
    chmod($file_path,0777))     $message "ERROR: A folder to place the files was not found, or the files need to be CHMODed to 777.";     
    else 
    $message = ($result)?"File has been uploaded." "An error has occurred.";     
    return 
    $message;  
    # PLACE AN IF STATEMENT HERE TO ENSIRE THAT THE FUNCTION WORKED #
    if (upload_file()) {
    header("Location: http://www.adifferentwebpage.com");
    exit();

    // end of function here 
    ?>
    Regards,
    BJ Duncan

  7. #7
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Duncan, this function is hardcoded and wont work ever, there are missing parameters and you are calling the function inside the function.
    my mobile portal
    ghiris.ro

  8. #8
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are calling the function inside the function.
    Spot on! - Thanks Ernie.

    Alex111, My apologies, Ernie is spot on, if you fix up the parameters, you can then fix the 'if statement' in my previous post AFTER you call the function in the main script.

    (Hope it's right this time?)
    Regards,
    BJ Duncan

  9. #9
    Non-Member
    Join Date
    Apr 2007
    Posts
    224
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Thanks for your replies.

    Can you please elaborate on this statement? I'm not clear on it.

    "if you fix up the parameters, you can then fix the 'if statement' in my previous post AFTER you call the function in the main script"


    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •