2016-12-01 13 views
-1

アップロード中に写真の名前を変更したいと思います。ここでアップロード中に画像の名前を変更する

は私のコードです:

if(isset($_POST['submit'])) 
{ 
    $target_dir = "pictures/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

    // Allow certain file formats 
    if($imageFileType != "gif") { 
     echo "Sorry only GIF files are allowed."; 
     $uploadOk = 0; 
    } 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 
     echo "Sorry, your file was not uploaded."; 
    // if everything is ok, try to upload file 
    } else { 
     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
      echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
      header('Location:field_medal_winner.php'); 
     } else { 
      echo "Sorry, there was an error uploading your file."; 
     } 
    } 
} 

まず私はmove_uploaded_file機能を変更whithそれを実行しようとしました。 しかし、私はちょうど他のメッセージだ:

if (move_uploaded_file("text.gif", $target_file)) { 
    echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
} else { 
    echo "Sorry, there was an error uploading your file."; 
} 

を誰もが私が間違っているの何のアイデアを持っていますか?

+0

あなたがヘッダの前に出力している - それhttp://php.net/manual/en/function.error-reporting.phpとフォームの不明を確認してください。 * "しかし、エラーがあるので、私はちょうどelseメッセージ" *を得ました。 –

+0

[PHPで「ヘッダーが既に送信されました」というエラーを修正する方法](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) –

+1

可能[アップロードしたファイルをディレクトリに保存する前に名前を変更する方法は?](http://stackoverflow.com/questions/18705639/how-to-rename-uploaded-file-before-saving-it-a-directory ) –

答えて

0

それはあなたの目的のフォルダに text.gif として保存されます

$target_file = $target_dir . "text.gif"; 
$imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION); 

ではこれらの2行

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

を交換してください。しかし、あなたがアップロードするファイルは何でも、それは古いものを上書きするでしょうtext.gifファイルにtext.gif同じ名前のファイルがアップロードされます。

更新されたコード

if(isset($_POST['submit'])) 
{ 
    $target_dir = "pictures/"; 
    $target_file = $target_dir . "text.gif"; 
    $uploadOk = 1; 
    $imageFileType = pathinfo($_FILES["fileToUpload"]["name"],PATHINFO_EXTENSION); 

    // Allow certain file formats 
    if(($imageFileType != "gif") || ($imageFileType != "GIF")) { 
    echo "Sorry only GIF files are allowed."; 
    $uploadOk = 0; 
    } 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
    } else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
     header('Location:field_medal_winner.php'); 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
    } 
} 
+0

ちょうどあなたの助けのためにちょうど良いタンクを動作させる! –

+0

@AaronWerlen:この答えを正解と記すことを忘れないでください。として、それは他のユーザーが簡単にこの答えを見つけるのに役立ちます。回答を適切なものとしてマークする方法がわからない場合は、このリンクをチェックしてください。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

関連する問題