1

I have a piece of javascript that uses Ajax and PHP to upload an image.

I am using the javascript in the head section of a few html pages and was wondering if there was a way of passing a value from these pages to the Upload.php file, without having to create individual Upload.php files for each web page.

Section of the javascript:

<script type="text/javascript" >
        $(function(){
            var btnUpload=$('#me');
            var mestatus=$('#mestatus');
            var files=$('#files');
            new AjaxUpload(btnUpload, {
                action: '../UploadDoc/Upload.php',
                name: 'uploadfile',

Section of the Upload.php file i would like to pass value to:

<?php
/* Set the location to upload the file to */
$uploaddir = '../documents/userdocs/'; 

/* Set the name and file extension of the file being saved */
$file = $uploaddir ."HtmlPage1_".basename($_FILES['uploadfile']['name']); 
$file_name= "HtmlPage1_".$_FILES['uploadfile']['name']; 

Where 'HtmlPage1_' will be the information i need to pass in, as it is the only part that changes.

Any help much appreciated.

Many thanks

1 Answers1

1

In your Javascript do

$(function(){
        var btnUpload=$('#me');
        var mestatus=$('#mestatus');
        var files=$('#files');
        new AjaxUpload(btnUpload, {
            action: '../UploadDoc/Upload.php',
            name: 'uploadfile',
            params: {
                  pageKey: 'HtmlPage1_'
            }
      ......ETC-CODE......................

OR your javascript code do

$(function(){
        var btnUpload=$('#me');
        var mestatus=$('#mestatus');
        var files=$('#files');
        new AjaxUpload(btnUpload, {
            action: '../UploadDoc/Upload.php?pageKey=HtmlPage1_',
            name: 'uploadfile',
      ......ETC-CODE......................

And in your PHP do :

<?php
/* Set the location to upload the file to */
$uploaddir = '../documents/userdocs/'; 

/* Set the name and file extension of the file being saved */
$file = $uploaddir .$_REQUEST['pageKey'].basename($_FILES['uploadfile']['name']); 
$file_name= $_REQUEST['pageKey'].$_FILES['uploadfile']['name']; 
......ETC-CODE......................

Then you modify your javascript code in each page to a "pageKey" parameter.

Cyril Tourist
  • 551
  • 5
  • 13
  • Thanks for your reply. Unfortunately, although the file is still being uploaded, the param value doesn't seem to be reaching the PHP file. The file is saved with its normal name, minus the intended 'HtmlPage1_' prefix. Any idea why this could be? Let me know if posting more of the code would help. – Daniel Andrews May 30 '12 at 20:29
  • Another way would be to do – Cyril Tourist May 30 '12 at 21:06