weixin_33724659 2012-05-30 19:02 采纳率: 0%
浏览 36

文件上传Ajax PHP

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条回答 默认 最新

  • weixin_33676492 2012-05-30 19:30
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)