Advertisement
ssaidz

upload.php - upload via url and rename

Nov 29th, 2023
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. <?php
  2. if(isset($_POST['submit'])){
  3.   $file_url = $_POST['file_url'];
  4.   $new_name = $_POST['new_name'];
  5.  
  6.   // Inisialisasi cURL
  7.   $ch = curl_init($file_url);
  8.  
  9.   // Menentukan opsi untuk menyimpan hasil download ke variabel
  10.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  11.  
  12.   // Melakukan request
  13.   $data = curl_exec($ch);
  14.  
  15.   // Menutup koneksi cURL
  16.   curl_close($ch);
  17.  
  18.   // Mendapatkan informasi nama file asli jika `<link>$new_name</link>` kosong
  19.   if(empty($new_name)){
  20.     $path_parts = pathinfo($file_url);
  21.     $new_name = $path_parts['basename'];
  22.   }
  23.  
  24.   // Menyimpan file ke folder uploads dengan nama baru
  25.   if(file_put_contents('uploads/' . $new_name, $data) !== false){
  26.     $file_url = 'http://' . $_SERVER['HTTP_HOST'] . '/uploads/' . $new_name;
  27.     $file_size = filesize('uploads/' . $new_name);
  28.     $file_size_mb = round($file_size/1024/1024, 2); // Mengonversi ukuran file ke MB
  29.     echo "File berhasil diunggah. <br>";
  30.     echo "Nama file: <link>$new_name</link> <br>";
  31.     echo "Ukuran file: $file_size_mb MB <br>";
  32.     echo "Link: <a href='$file_url'>$file_url</a>";
  33.   } else {
  34.     echo "Gagal mengunggah file.";
  35.   }
  36. }
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement