前回の続き。
アップロードを受け付けるプログラム(upload.php)のメモ
- $_FILESは何?
- empty()
- time()
- hash()
- dirname(__FILE__)
- move_uploaded_file()
- ${title}
$app_url_base = "PATH_TO_UPLOADER/sketchbook/";
$data_file = "sketch.dat";
$image_dir = "image";
$error = null;
$data = array();
$title = $_POST['title'];
$file = $_FILES['image'];
$tmp_name = $file['tmp_name'];
if (empty($title)) {
header("HTTP/1.0 400 Bad Request");
$data = array("error" => "title is required.");
echo json_encode($data);
exit;
}
$date = time();
$file_name = hash('md5', $title, $date) . ".png";
$file_path = dirname(__FILE__) . "/" . $image_dir . "/" . $file_name;
$result = @move_uploaded_file($tmp_name, $file_path);//
if ($result === true) {
$fp = @fopen($data_file, "a");
if ($fp !== false) {
$line = "${title},${date},${file_name}n";
$result = @fputs($fp, $line);
if ($result === false) {
$error = "failed to register(file write).";
}
}
else {
$error = "failed to register(file open).";
}
}
else {
$error = "failed to upload.";
}
if ($error !== null) {
header("HTTP/1.0 500 Internal Server Error");
$data = array("error" => $error);
}
else {
$data = array("result" => "Upload OK!");
}
echo json_encode($data);
参考サイト
- iOS/Android/Windows Phone プログラミング – 日経ソフトウエア バックナンバー購入:ITpro
iOS/Android/Windows Phoneプログラミングのサイト(サンプルコードのダウンロードが可能)
iOS/Android/Windows Phoneプログラミング (日経BPパソコンベストムック)
posted with amazlet at 12.03.16
日経BP社 (2012-01-12)
売り上げランキング: 64019
売り上げランキング: 64019
関連エントリー
