2017-01-06 3 views
0

アップロードしたファイルへの相対パスを取得するにはどうすればよいですか?たとえば、私がtest.pngをアップロードすると、私は/upload/test.pngを得るでしょう。ここに私のHTMLです:アップロードされたファイルの相対パスをphp(repost)で変数として取得する方法

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Jquery Ajax File Upload</title> 
</head> 
<body> 

    <div class="col-sm-6"> 
     <div class="form-group "> 
      <label>Profile image</label> 
      <div class="input-group"> 
       <span class="input-group-addon"><i class="fa fa-image"></i></span> 
       <input type="text" class="form-control" name="profile_image" autocomplete="off" value="" placeholder="" > 
       <label class="btn btn-default btn-file input-group-addon"> 
    Browse <input type="file" name="image" style="display: none;" onchange="myFunction()" id="image" > 
</label> 
<div class="result"></div> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <script> 

     $('#image').change(function(e){ 
      var file = this.files[0]; 
      var form = new FormData(); 
      form.append('image', file); 
      $.ajax({ 
       url : "http://192.168.1.147/upload.php", 
       type: "POST", 
       cache: false, 
       contentType: false, 
       processData: false, 
       data : form, 
       success: function(response){ 
        $('.result').html(response.html) 
       } 
      }); 
     }); 

    </script> 

      </div> 
     </div> 
    </div> 

</body> 
</html> 

PHP:コードが動作する

<?php 

$file = $_FILES['image']; 


/* Allowed file extension */ 
$allowedExtensions = ["gif", "jpeg", "jpg", "png", "svg"]; 

$fileExtension = explode(".", $file["name"]); 

/* Contains file extension */ 
$extension = end($fileExtension); 

/* Allowed Image types */ 
$types = ['image/gif', 'image/png', 'image/x-png', 'image/pjpeg', 'image/jpg', 'image/jpeg','image/svg+xml']; 

if(in_array(strtolower($file['type']), $types) 
    // Checking for valid image type 
    && in_array(strtolower($extension), $allowedExtensions) 
    // Checking for valid file extension 
    && !$file["error"] > 0) 
    // Checking for errors if any 
    { 
    if(move_uploaded_file($file["tmp_name"], 'uploads/'.$file['name'])){ 
     header('Content-Type: application/json'); 
     echo json_encode(['html' => /*return uploded file path and name*/ ]);  
     echo $_FILES['upload']['name']; 
    }else{ 
     header('Content-Type: application/json'); 
     echo json_encode(['html' => 'Unable to move image. Is folder writable?']);  
    } 
}else{  
    header('Content-Type: application/json'); 
    echo json_encode(['html' => 'Please upload only png, jpg images']); 
} 

?> 

、それは、ファイルをアップロードしているが、私は戻ってパスを取得する方法がわかりません。パスはユーザープロファイルイメージのために変更され、後で/ $ usernameというアップロードパスに変更されます。とにかく名前を知っていれば、とにかく投稿してください。前もって感謝します。

答えて

0

$file['name']は、その中のファイルの名前を持っている。これは、特定の場所にファイルを保存し、プログラムのビットであるtest1.png

言うことができます。この場合、現在の作業ディレクトリに対して相対的なフォルダuploadsがあります。

if(move_uploaded_file($file["tmp_name"], 'uploads/'.$file['name'])){ 
    header('Content-Type: application/json'); 
    echo json_encode(['html' => 
         /* you might want to output something like */ 
          getcwd().'/uploads/'.$file['name'] 
         // getcwd() current working directory 
         // it may be that you need a relative path based on 
         // where your application lives to generate a url, for example 
         // 'http://your_app_base_url/'.'/uploads/'.$file['name'] 
        ]);  
    echo $_FILES['upload']['name']; // this will be empty unless there is a file form field with the name 'upload' 
            // compare with $_FILE['image'] above 
}else{ 
    header('Content-Type: application/json'); 
    echo json_encode(['html' => 'Unable to move image. Is folder writable?']);  
} 

後でファイルを移動したい場合は、profile_picsというディレクトリがあり、そこに移動し、ユーザー名に名前を変更することができます。

$oldfilename = $file['name']; 

//create a name for the file based on username and uploaded file extension (.jpg/.png) 
$newfilename = $username.'_profile.'.pathinfo($oldfilename,PATHINFO_EXTENSION); 

//this will rename /move the file from uploads to profile_pics (directory must exist already) 
rename('uploads/'.$oldfilename,'profile_pics/'.$newfilename); 
+0

ありがとうございました。私はちょうどgetcwd()を削除しました。これは、フォワードスラッシュとバックスラッシュで完全パスを返したためです。C:\ xampp \ root \ public/uploads / –

関連する問題