May 092016
 

Upload FIle with delphi and php

Assalamu’ alaikum wr wb

Upload File

Sekarang kita akan membahas sedikit aplikasi kecil untuk proses upload file dengan menggunakan delphi 7 sebagai client side nya dan php sebagai server side nya.

langsung saja buat form seperti pada gambar di atas.

lalu tulis stricpt di bawah ini :


unit UploadFile;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdMultipartFormData, IdHTTP, HTTPApp, ExtCtrls,
OleCtrls, SHDocVw;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Panel1: TPanel;
Button2: TButton;
Button1: TButton;
Edit1: TEdit;
WebBrowser1: TWebBrowser;
ComboBox1: TComboBox;
Edit2: TEdit;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text := OpenDialog1.FileName;
//OpenDialog1.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TStringStream;
Params: TIdMultipartFormDataStream;
HTTP: TIdHTTP;
url : String;
begin
{
Gambar
Excel
Word
Pdf
Other
}
Case ComboBox1.ItemIndex Of
0 : Begin
url := 'images';
end;
1 : begin
url := 'xls';
end;
2 : begin
url := 'doc';
end;
3 : begin
url := 'pdf';
end;
4 : begin
url := 'other';
end;
end;
Stream := TStringStream.Create('');
try
Params := TIdMultipartFormDataStream.Create;
try
Params.AddFile('fileToUpload', Edit1.Text ,'application/octet-stream');
try
HTTP := TIdHTTP.Create(Self);
HTTP.Post(Edit2.Text + '/upload_' + url + '.php', Params, Stream);
WebBrowser1.Navigate(Edit2.Text + '/upload-' + url);
//WebBrowser1.Refresh;
except
on E: Exception do
ShowMessage('Error encountered during POST: ' + E.Message);
end;
ShowMessage(Stream.DataString);
finally
Params.Free;
end;
finally
Stream.Free;
HTTP.Destroy;
end;
end;

end.

Lalu buat program di sisi server untuk menerima file yang di upload oleh delphi.

berikut source codenya :


<?php
$target_dir = "upload-other/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if(file_exists($target_file)) {
unlink( $target_file);
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

simpan dengan nama : upload.php

Wassalamu’alaikum wr wb

Feb 142016
 

php convert base64 to image

Assalamu’alaikum wr wb

Bagaimana cara convert base64 menjadi file gambar, itu yang sering saya pikirkan beberapa hari ini. berikut akan saya bagikan cara convert dengan menggunakan php.

function base64_to_jpeg($base64_string, $output_file) {
        $ifp = fopen($output_file, "wb");
        $data = explode(',', $base64_string);
        if(count($data)>0){
            fwrite($ifp, base64_decode($data[1]));
        }else{
            fwrite($ifp, base64_decode($data[0]));
        }
        fclose($ifp);
        return $output_file;
}

Itu source yang bisa dipakai untuk menggunakannya seperti contoh di abah ini

base64_to_jpeg($_POST[‘string_base64’], ‘file.jpg’ )

Silakan dimanfaatkan sesuai dengan kebutuhan, semoga membantu.

Wassalamu’alaikum wr wb

Oct 282012
 

Assalamu’ alaikum wr wb

Yii upload images

Kali ini saya akan berbagi bagaimana cara upload gambar dengan menggunakan framework Yii (Yes It Is). Yii mempunyai banyak sekali extension yang dapat kita gunakan untuk mempermudah program yang akan kita buat. Untuk kali ini kita akan membuat upload image dengan menggunakan extension Upload yang sudah tersedia di Yii.

Yang di butuhkan :

  1. Apache.
  2. Yii Framework.
  3. Upload Extension.
Setelah download extension extrack semuake dalam folder extensions. Untuk penggunakan anda bisa membaca dalam link yang diats di upload extension. contoh :
// import the class
Yii::import('application.extensions.upload.Upload');

// receive file from post
$Upload = new Upload( (isset($_FILES['Filedata']) ? $_FILES['Filedata'] : null) );
$Upload->jpeg_quality  = 100;
$Upload->no_script     = false;
$Upload->image_resize  = true;
$Upload->image_x       = 700;
$Upload->image_y       = 500;
$Upload->image_ratio   = true;

// some vars
$newName  = date('YmdHis');
$destPath = Yii::app()->getBasePath().'/../images/gallery/';
$destName = '';

// verify if was uploaded
if ($Upload->uploaded) {
  $Upload->file_new_name_body = $newName;
  $Upload->process($destPath);

  // if was processed
  if ($Upload->processed) {
    $destName = $Upload->file_dst_name;

    // write image filename on table
    $photo = new Photo();
    $photo->gallery = $_POST['gallery_id'];
    $photo->image_normal = $destName;
    $photo->image_thumb  = 'thumb_' . $destName;
    $photo->save();

    // create the thumb
    unset($Upload);

    $Upload = new Upload($destPath.$destName);
    $Upload->file_new_name_body   = 'thumb_' . $newName;
    $Upload->no_script            = false;
    $Upload->image_resize         = true;
    $Upload->image_x              = 120;
    $Upload->image_y              = 80;
    $Upload->image_ratio          = true;
    $Upload->process($destPath);

  } else {
    echo($Upload->error)
  }
} else {
  echo('Select a file to send');
}
Ok selamat mencoba gitu saja ya untuk pengembangan silakan di buat sendiri ya.
Sumber : http://www.yiiframework.com

Wassalamu’ alaikum wr wb