画像をアップロードできるようにするこのスクリプトがあり、ポインタがファイルシステム上のファイルに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.";
}
?>
あなたの質問は正確ですか? –
お願いします。準備されたステートメントとバウンド変数について知ってください。あなたのスクリプトは、「SQLインジェクション」エクスプロイトに対応しています。 – Alnitak
mysql_real_escape_stringを使用しないと、コードがひどく安全でないことに注意してください。 $ name = $ _ POST ['nameMember']のような行だけを変更する必要があります。 $ name = mysql_real_escape_string($ _ POST ['nameMember']);より安全なものにするために長い道のりを歩む –