Please help me to add Image Uploading Path to both folders.Please help me to fix this below code:
Help me to fix define('UPLOAD_PATH', "How to enter both folders");
<?php
if( isset( $_GET['apicall'] ) ){
try{
$response = array();
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','indishare');
define('UPLOAD_PATH','images/thumb/');
$conn = new mysqli( DB_HOST, DB_USER, DB_PASS, DB_NAME ) or die('Unable to connect');
$apicall=trim( strtolower( $_GET['apicall'] ) );
switch( $apicall ){
case 'uploadpic':
if( isset( $_FILES['pic']['name'], $_POST['tags'] ) ){
$obj=(object)$_FILES['pic'];
$error=$obj->error;
$name=$obj->name;
$tmp=$obj->tmp_name;
$tags=$_POST['tags'];
if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){
$status = move_uploaded_file( $tmp, UPLOAD_PATH . $name );
if( $status ){
$sql='insert into `upload_img` (`image`, `tags` ) values (?,?)';
$stmt=$conn->prepare( $sql );
if( !$stmt )throw new Exception('failed to prepare sql insert statement');
$stmt->bind_param('ss', $name, $tags );
$result=$stmt->execute();
if( $result ){
$response['error'] = false;
$response['message'] = 'File uploaded successfully';
}
$stmt->close();
} else {
throw new Exception('failed to save file');
}
} else {
throw new Exception('Upload Error - Bad Foo!');
}
}
break;
//Get Pics
case 'getpics':
$sql='select `id`, `image`, `tags` from `upload_img`';
$stmt=$conn->prepare( $sql );
if( !$stmt )throw new Exception('failed to prepare sql select statement');
$result=$stmt->execute();
if( $result ){
$stmt->store_result();
$stmt->bind_result( $id, $image, $tags );
while( $stmt->fetch() ){
$upload_img[]=array(
'id' => $id,
'image' => 'http://' . $server_ip . ''. UPLOAD_PATH . $image,
'tags' => $tags
);
}
$stmt->close();
$response['error'] = false;
$response = $upload_img;
} else {
$response['error'] = true;
$response['message'] = 'No results returned by query';
}
break;
default:
throw new Exception( sprintf( 'Invalid api call - %s', $apicall ) );
break;
}
header('Content-Type: application/json');
echo json_encode( $response );
} catch( Exception $e ){
$response['error'] = true;
$response['message'] = $e->getMessage();
header('Content-Type: application/json');
exit( json_encode( $response ) );
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
}
?>
Jason Knight
The less code you use, the less there is to break
You don't. They're two separate folders so you need two separate operations... BUT, beware that move is a move, so what you need is a move AND a copy.
So you'd need TWO define.
define('UPLOAD_PATH_1','images/thumb/'); define('UPLOAD_PATH_2','images/other/');and then the move AND a copy. I would not do the copy unless the move succeeded:
$status = move_uploaded_file($tmp, UPLOAD_PATH_1 . $name); if ($status) { copy(UPLOAD_PATH_1 . $name, UPLOAD_PATH_2 . $name);Though I would be questioning why you'd want to waste disk space on two copies of the same file in a directory PHP (and therefor HTTP) has access to. link() might be a better choice than copy(), so there would in fact only one file and you are just creating virtual pointers to it -- but that would depend on your actual usage scenario.