php - Image upload/receive API -


i'd offer simple api on website allows people upload images (and receive url access it).

but have several questions:

  • would better if user have send image in binary code or better if user have send in idk ascii or so? conventional way? (i'm asking because can imagine languages have functions read files textfiles.)

  • where store images on server , how? can put them in mysql database , perform well? or should put them in 1 folder?

  • how should user able specify file type? in header or in body?

  • how receive , data save file?

i found code somewhere else:

<?php  /* put data comes in on stdin stream */ $putdata = fopen("php://input", "r");  /* open file writing */ $fp = fopen("myputfile.ext", "w");  /* read data 1 kb @ time    , write file */ while ($data = fread($putdata, 1024))   fwrite($fp, $data);  /* close streams */ fclose($fp); fclose($putdata);  ?> 

does work binary files images?
idea "read data 1 kb @ time"?

the easiest way store file in folder on server. store url of file in mysql database, , pull file url (assuming have login function) if user not know file location.

for instance:

(upload.php or script using upload file server)

<?php     $connect_to_db = mysqli_connect('localhost', 'user', 'pass', 'db');      $user = $_post['user'];      $allowedexts = array("gif", "jpeg", "jpg", "png");     $temp = explode(".", $_files["file"]["name"]);     $extension = end($temp);      if ((($_files["file"]["type"] == "image/gif")     || ($_files["file"]["type"] == "image/jpeg")     || ($_files["file"]["type"] == "image/jpg")     || ($_files["file"]["type"] == "image/pjpeg")     || ($_files["file"]["type"] == "image/x-png")     || ($_files["file"]["type"] == "image/png"))     && in_array($extension, $allowedexts)) {        if ($_files["file"]["error"] > 0) {          echo "error: " . $_files["file"]["error"] . "<br>";        } else {          //move file uploads folder         move_uploaded_file($_files["file"]["tmp_name"], "uploads/" . $_files["file"]["name"]);          //get file location         $filelocation = 'http://yourdomain.com/uploads/'.$_files["file"]["name"];          //get file size         $size = ($_files["file"]["size"]/1024).' kb';          //save database         mysqli_query($connect_to_db, "insert images (user, filelocation, size) values ('$user', '$filelocation', '$size')");          //redirect confirmation page, , include file location in url         header('location: confirm.php?location='.$filelocation);       }     } else {       //file type invalid, throw red flag!       echo "invalid file type";     } ?> 

now can make table in page, , have list uploaded files based on logged in (again, assuming use feature). allow person know have keep log of files upload.

the following example if posting data using ajax, , returns json formatted data, can have user not have reload page.

<?php     $connect_to_db = mysqli_connect('localhost', 'user', 'pass', 'db');      $user = $_post['user'];      $allowedexts = array("gif", "jpeg", "jpg", "png");     $temp = explode(".", $_files["file"]["name"]);     $extension = end($temp);      if ((($_files["file"]["type"] == "image/gif")     || ($_files["file"]["type"] == "image/jpeg")     || ($_files["file"]["type"] == "image/jpg")     || ($_files["file"]["type"] == "image/pjpeg")     || ($_files["file"]["type"] == "image/x-png")     || ($_files["file"]["type"] == "image/png"))     && in_array($extension, $allowedexts)) {        if ($_files["file"]["error"] > 0) {          echo json_encode(array('status' => 'error', 'msg' => 'file not uploaded.'));        } else {          //move file uploads folder         move_uploaded_file($_files["file"]["tmp_name"], "uploads/" . $_files["file"]["name"]);          //get file location         $filelocation = 'http://yourdomain.com/uploads/'.$_files["file"]["name"];          //get file size         $size = ($_files["file"]["size"]/1024).' kb';          //save database         mysqli_query($connect_to_db, "insert images (user, filelocation, size) values ('$user', '$filelocation', '$size')");          //return data in json format         echo json_encode(array('status' => 'success', 'data' => array('filelocation' => $filelocation, 'size' => $size)));       }     } else {       //file type invalid, throw red flag!       echo json_encode(array('status' => 'error', 'msg' => 'invalid file format'));     } ?> 

if restrict file size, can add simple line of code check file size, , if big, won't let go through:

if ((($_files["file"]["type"] == "image/gif") || ($_files["file"]["type"] == "image/jpeg") || ($_files["file"]["type"] == "image/jpg") || ($_files["file"]["type"] == "image/pjpeg") || ($_files["file"]["type"] == "image/x-png") || ($_files["file"]["type"] == "image/png")) && ($_files["file"]["size"] < 20000) //must smaller 20kb && in_array($extension, $allowedexts)) { 

if need more help, feel free let me know. :)


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -