2011-01-18 14 views
3

画像をアップロードできるようにするこのスクリプトがあり、ポインタがファイルシステム上のファイルにDBに保存されています。私は別のStackOverflowの質問からこのスクリプトを手に入れました。それは2つのことを除いて私が必要とするすべてのことをしています。 最初に私が必要とするのは、画像のサイズを変更できるようにすることです。ちょうど私が読んだ2番目の部分は、ユーザーのエラーなどを防ぐためにファイルの名前を変更することです。GDファイルのサイズを変更し、PHP画像アップロードスクリプトに名前を変更

ここでは、ユーザ入力フォームを作成し、DBおよび画像にデータを画像ディレクトリに書き込む。

<?php 

//This is the directory where images will be saved 
$target = "your directory"; 
$target = $target . basename($_FILES['photo']['name']); 

//This gets all the other information from the form 
$name=$_POST['nameMember']; 
$bandMember=$_POST['bandMember']; 
$pic=($_FILES['photo']['name']); 
$about=$_POST['aboutMember']; 
$bands=$_POST['otherBands']; 


// Connects to your Database 
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ; 
mysql_select_db("dbName") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands) 
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 
+0

あなたの質問は正確ですか? –

+2

お願いします。準備されたステートメントとバウンド変数について知ってください。あなたのスクリプトは、「SQLインジェクション」エクスプロイトに対応しています。 – Alnitak

+0

mysql_real_escape_stringを使用しないと、コードがひどく安全でないことに注意してください。 $ name = $ _ POST ['nameMember']のような行だけを変更する必要があります。 $ name = mysql_real_escape_string($ _ POST ['nameMember']);より安全なものにするために長い道のりを歩む –

答えて

0

サイズ変更については、GDを使用して画像を任意のサイズにサイズ変更する方法を説明する記事です。目的地のアスペクト比に合わせてクロッピングやレターボクシングのオプションがあります。

http://www.spotlesswebdesign.com/blog.php?id=1

は、ファイル名の場合は、単にランダムIDにファイルの名前を変更し、すでにそのIDを使用していないことを確認してください。

do { 
    $filename = uniqid().'jpg'; 
} while (file_exists('image/path/'.$filename); 
+0

そして@Alnitakは正しいです、SQLインジェクションについて学んでください... – dqhendricks

関連する問題