Sunday, 15 April 2012

php - Save file location on upload -



php - Save file location on upload -

i having problem uploading file in php. need save file's location in db, won't work. also, want rename file equal timestamp + user's name. have users name stored in variable.

most importantly though, how post file's location db?

edit

right now, i'm getting file upload folder on server, can't save file's location string on db. want able reference file later, don't know how save file's location later use.

$ext_img_2 = end(explode(".", $_files["img_2"]["name"])); if ((($_files["img_2"]["type"] == "image/gif") || ($_files["img_2"]["type"] == "image/jpeg") || ($_files["img_2"]["type"] == "image/png") || ($_files["img_2"]["type"] == "image/pjpeg")) && ($_files["img_2"]["size"] < 6291456) && in_array($ext_img_2, $allowedexts)) { if ($_files["img_2"]["error"] > 0) { echo "return code: " . $_files["img_2"]["error"] . "<br>"; } else { if (file_exists("adimg2/" . $_files["img_2"]["name"])) { echo $_files["img_2"]["name"] . " exists. " . "<br>"; } else { move_uploaded_file($_files["img_2"]["tmp_name"], "adimg2/" . $_files["img_2"]["name"]); mysql_query("insert info (img_2) values ('adimg1/".$_files['img_1']['name']."')"; echo "stored in: " . "adimg2/" . $_files["img_2"]["name"] . "<br>"; } } } else { echo "invalid file" . "<br>"; }

i recommend utilize unique name uploaded file (with function uniqid http://php.net/manual/fr/function.uniqid.php ) , utilize original file extention if needed. illustration :

$name = uniqid() . '.' . pathinfo($filename, pathinfo_extension); $name = time() . '.' . pathinfo($filename, pathinfo_extension); // timestamp

then, perform tour tests , :

$path = 'uploadedfilesdir/' . $name; move_uploaded_file($_files["img_2"]["tmp_name"], $path); mysql_query('insert data(...) values ("' . $path . '")');

simple (pay attending directory rights, php may not able write in directory).

php post upload

No comments:

Post a Comment