dongwu8050
2014-03-08 02:32
浏览 330
已采纳

如何在另一个php文件中调用变量值

I have an upload script which uploads files to hosting and the redirects to success urlcode of upload script is as below. I want to get value from this to success url.

<?php

// Folder to upload files to. Must end with slash /
define('DESTINATION_FOLDER',$_SERVER['DOCUMENT_ROOT'].'/upload/');

// Maximum allowed file size, Kb
// Set to zero to allow any size
define('MAX_FILE_SIZE', 0);

// Upload success URL. User will be redirected to this page after upload.
define('SUCCESS_URL','http://sap.layyah.info/result.php');

// Allowed file extensions. Will only allow these extensions if not empty.
// Example: $exts = array('avi','mov','doc');
$exts = array();

// rename file after upload? false - leave original, true - rename to some unique filename
define('RENAME_FILE', false);

// put a string to append to the uploaded file name (after extension);
// this will reduce the risk of being hacked by uploading potentially unsafe files;
// sample strings: aaa, my, etc.
define('APPEND_STRING', '');

####################################################################

END OF SETTINGS.   DO NOT CHANGE BELOW
####################################################################

// Allow script to work long enough to upload big files (in seconds, 2 days by default)
@set_time_limit(172800);

// following may need to be uncommented in case of problems
// ini_set("session.gc_maxlifetime","10800");

function showUploadForm($message='') {
$max_file_size_tag = '';
if (MAX_FILE_SIZE > 0) {
// convert to bytes
$max_file_size_tag = "<input name='MAX_FILE_SIZE' value='".(MAX_FILE_SIZE*1024)."' type='hidden' >
";
}

// Load form template
include ('file-upload.html');
}

// errors list
$errors = array();

$message = '';

// we should not exceed php.ini max file size
$ini_maxsize = ini_get('upload_max_filesize');
if (!is_numeric($ini_maxsize)) {
if (strpos($ini_maxsize, 'M') !== false)
$ini_maxsize = intval($ini_maxsize)*1024*1024;
elseif (strpos($ini_maxsize, 'K') !== false)
$ini_maxsize = intval($ini_maxsize)*1024;
elseif (strpos($ini_maxsize, 'G') !== false)
$ini_maxsize = intval($ini_maxsize)*1024*1024*1024;
}
if ($ini_maxsize < MAX_FILE_SIZE*1024) {
$errors[] = "Alert! Maximum upload file size in php.ini (upload_max_filesize) is less than script's MAX_FILE_SIZE";
}

// show upload form
if (!isset($_POST['submit'])) {
showUploadForm(join('',$errors));
}

// process file upload
else {

while(true) {

// make sure destination folder exists
if (!@file_exists(DESTINATION_FOLDER)) {
  $errors[] = "Destination folder does not exist or no permissions to see it.";
  break;
}

// check for upload errors
$error_code = $_FILES['filename']['error'];
if ($error_code != UPLOAD_ERR_OK) {
  switch($error_code) {
    case UPLOAD_ERR_INI_SIZE: 
      // uploaded file exceeds the upload_max_filesize directive in php.ini
      $errors[] = "File is too big (1).";
      break;
    case UPLOAD_ERR_FORM_SIZE: 
      // uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form
      $errors[] = "File is too big (2).";
      break;
    case UPLOAD_ERR_PARTIAL:
      // uploaded file was only partially uploaded.
      $errors[] = "Could not upload file (1).";
      break;
    case UPLOAD_ERR_NO_FILE:
      // No file was uploaded
      $errors[] = "Could not upload file (2).";
      break;
    case UPLOAD_ERR_NO_TMP_DIR:
      // Missing a temporary folder
      $errors[] = "Could not upload file (3).";
      break;
    case UPLOAD_ERR_CANT_WRITE:
      // Failed to write file to disk
      $errors[] = "Could not upload file (4).";
      break;
    case 8:
      // File upload stopped by extension
      $errors[] = "Could not upload file (5).";
      break;
  } // switch

  // leave the while loop
  break;
}

// get file name (not including path)
$filename = @basename($_FILES['filename']['name']);

// filename of temp uploaded file
$tmp_filename = $_FILES['filename']['tmp_name'];

$file_ext = @strtolower(@strrchr($filename,"."));
if (@strpos($file_ext,'.') === false) { // no dot? strange
  $errors[] = "Suspicious file name or could not determine file extension.";
  break;
}
$file_ext = @substr($file_ext, 1); // remove dot

// check file type if needed
if (count($exts)) {   /// some day maybe check also $_FILES['user_file']['type']
  if (!@in_array($file_ext, $exts)) {
    $errors[] = "Files of this type are not allowed for upload.";
    break;
  }
}

// destination filename, rename if set to
$dest_filename = $filename;
if (RENAME_FILE) {
  $dest_filename = md5(uniqid(rand(), true)) . '.' . $file_ext;
}
// append predefined string for safety
$dest_filename = $dest_filename . APPEND_STRING;

// get size
$filesize = intval($_FILES["filename"]["size"]); // filesize($tmp_filename);

// make sure file size is ok
if (MAX_FILE_SIZE > 0 && MAX_FILE_SIZE*1024 < $filesize) {
  $errors[] = "File is too big (3).";
  break;
}

if (!@move_uploaded_file($tmp_filename , DESTINATION_FOLDER . $dest_filename)) {
  $errors[] = "Could not upload file (6).";
  break;
}


// redirect to upload success url
header('Location: ' . SUCCESS_URL);
die();

break;

} // while(true)

// Errors. Show upload form.
$message = join('',$errors);
showUploadForm($message);

}

?>

I want to show $dest_filename on SUCCESS_URL I have tried include and echo but it is not working

In Success URL I am Using:

<?php
if(isset($_GET['uploaded_file_url'])){
    $var_1 = $_GET['uploaded_file_url'];
    echo $var_1;
}
    echo '<a href="'.$var_1.'">Click here for uploaded file!</a>;
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongshuobei1037 2014-03-08 02:37
    已采纳

    Is this what you're after??

    // redirect to upload success url
    header('Location: '.SUCCESS_URL."?uploaded_file_url=".DESTINATION_FOLDER."/".$dest_filename);
    die();
    

    In your successful url script put this at the top of your page:

    if(isset($_GET['uploaded_file_url'])){
        $var_1 = $_GET['uploaded_file_url'];
        echo $var_1;
    }
    

    If you want a hyperlink to the url of that file (for download etc), then just wrap a tags around it like so:

    echo '<a href="'.$var_1.'">Click here for uploaded file!</a>;
    
    点赞 评论
  • douyingmou1389 2014-03-08 02:50

    Try to use POST and GET to pass the string on the URL.

    点赞 评论

相关推荐 更多相似问题