Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Pahelp po sa multiple upload

SassyBoy

Novice
Advanced Member
Messages
32
Reaction score
0
Points
26
public function uploadFile($data)
{

if($_FILES['file']){
$file_name = $_FILES['file']['name'][0];
$file_tmp=$_FILES['file']['tmp_name'][0];
$file_type=$_FILES['file']['type'][0];
$file_size =$_FILES['file']['size'][0];

$allowedExts = array("jpeg", "jpg", "png");
$temp = strtolower(end(explode(".", $file_name)));

$extension = $temp;

if ((($file_type == "image/gif")
|| ($file_type == "image/jpeg")
|| ($file_type == "image/jpg")
|| ($file_type == "image/pjpeg")
|| ($file_type == "image/x-png")
|| ($file_type == "image/png")
|| ($file_type == "image/png")
|| ($file_type == "application/pdf")
|| ($file_type == "application/msword"))
&& ($file_size <= 5242880)){

if(file_exists($data["path"])){
}
else{
mkdir($path,0777,true);
}

move_uploaded_file($file_tmp, $data["path"].$data["user"] ."_".$file_name."_".$data["type"]."_".date("Ymd").".". $extension);
$data["path"] = $data["path"].$data["user"] ."_".$file_name."_".$data["type"]."_".date("Ymd").".". $extension;


$params = array(
'u_id' => $data["user"],
'type' => $data["type"],
'file_path' => $data["path"],
'file_type' => $file_type,
'file_name' => $file_name,
'file_size' => $file_size,
'file_modifiedby' => $data["modifiedby"]
);

$result = MySQL_TristanGaryLeyesa_Framework::mysql_insert($data["database"],$data["table"],$params);
if($result){
$arr = array('name' => $file_name,
'size'=> $file_size,
'url' => "http://122.54.107.118:1992/USI/".$data["path"],
'thumbnailUrl' => "http://122.54.107.118:1992/USI/system".$data["path"],
'type' => $file_type,
'deleteUrl' => "http://122.54.107.118:1992/USI/system".$data["path"],
'deleteType' => "DELETE");

$filejson->files[] = $arr;

return json_encode($filejson);
}
return array("message" => "Failed to Upload File",
"status" => false);

}
else{
return array("message" => "Failed to Upload File",
"status" => false);
}
}
}

pahelp naman po sa mga experts jan. eto po yung function ko. pano ko po ba ggwin na for multiple upload ito? san ko po ba isisingit yung foreach? salamat po!
 
try lang peru ganito ang idea ko may mas maganda pa dito


public function uploadFile($data)
{


$allowedExts = array("jpeg", "jpg", "png");

if($_FILES['file'])
{

for($i=0; $i < count ($_FILES['file']); $i++)
{
$file_name[$i] = $_FILES['file']['name'][0];
$file_tmp[$i] =$_FILES['file']['tmp_name'][0];
$file_type[$i] =$_FILES['file']['type'][0];
$file_size[$i] =$_FILES['file']['size'][0];
$temp[$i] = strtolower(end(explode(".", $file_name[$i] )));
$extension[$i] = $temp[$i];

if ((($file_type[$i] == "image/gif")
|| ($file_type[$i] == "image/jpeg")
|| ($file_type[$i] == "image/jpg")
|| ($file_type[$i] == "image/pjpeg")
|| ($file_type[$i] == "image/x-png")
|| ($file_type[$i] == "image/png")
|| ($file_type[$i] == "image/png")
|| ($file_type[$i] == "application/pdf")
|| ($file_type[$i] == "application/msword"))
&& ($file_size[$i] <= 5242880))
{
if(file_exists($data["path"])){
}
else
{
$path='uploads/';//sample

mkdir($path,0777,true); //etong part na to $path anung value nyan dmo na set edeclare mo muna
}


move_uploaded_file($file_tmp[$i], $data["path"].$data["user"] ."_".$file_name[$i]."_".$data["type"]."_".date("Ymd").".". $extension[$i]);
$data["path"] = $data["path"].$data["user"] ."_".$file_name[$i]."_".$data["type"]."_".date("Ymd").".". $extension[$i];

$params[$i] = array(
'u_id' => $data["user"],
'type' => $data["type"],
'file_path' => $data["path"],
'file_type' => $file_type[$i],
'file_name' => $file_name[$i],
'file_size' => $file_size[$i],
'file_modifiedby' => $data["modifiedby"]
);



}


for($e=0; $e < count($params); $e++;) //pwede ren to ipasok sa unang for loop para hindi na dalawa
{

$result = MySQL_TristanGaryLeyesa_Framework::mysql_insert($data["database"],$data["table"],$params[$e]);

if($result)
{ //possible dti mag ka error magulo

$arr = array('name' => $file_name[$e],
'size'=> $file_size[$e],
'url' => "http://122.54.107.118:1992/USI/".$data["path"],
'thumbnailUrl' => "http://122.54.107.118:1992/USI/system".$data["path"],
'type' => $file_type[$e],
'deleteUrl' => "http://122.54.107.118:1992/USI/system".$data["path"],
'deleteType' => "DELETE");

$filejson->files[] = $arr;

return json_encode($filejson);
}
return array("message" => "Failed to Upload File",
"status" => false);

}
else
{
return array("message" => "Failed to Upload File",
"status" => false);
}

}

}

}//mga close curly brace dko na nacheck kung tama
 
try mo gumamit ng class.upload.php na class mas madali https://github.com/verot/class.upload.php

Sample code

index.php
Code:
<form action="upload.php" method="post" enctype="multipart/form-data">

<label> Image 1 </label>
<input type="file" name="image[]"/>

<label> Image 2 </label>
<input type="file" name="image[]"/>

<label> Image 3 </label>
<input type="file" name="image[]"/>

  <input type="submit" value="Submit">
</form>

upload.php

Code:
<?php

require_once 'class.upload.php';

$files = array();
foreach ($_FILES['image'] as $k => $l) {
  foreach ($l as $i => $v) {
  if (!array_key_exists($i, $files))
    $files[$i] = array();
    $files[$i][$k] = $v;
  }
}


foreach ($files as $file) {
  $handle = new Upload($file);
  if ($handle->uploaded) {
    $handle->Process("./");
    if ($handle->processed) {
      echo 'OK';
    } else {
      echo 'Error: ' . $handle->error;
    }
  } else {
    echo 'Error: ' . $handle->error;
  }
  unset($handle);
}
?>

class.upload.php source code
https://raw.githubusercontent.com/verot/class.upload.php/master/src/class.upload.php
 
Last edited:
try lang peru ganito ang idea ko may mas maganda pa dito

sir nag uunexpected else po sya dun sa last na else tska hindi padin sya nag iinsert as multiple. ung first image lang napasok
 
Last edited:
subukan mo to!


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<p><input type="file" name="file_array[]"></p>
<input type="submit" value="Upload all files">
</form>
</body>
</html>
<?php

if(isset($_FILES['file_array']))
{

$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];

for($i = 0; $i < count($tmp_name_array); $i++)
{
if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i]))
{
echo $name_array[$i]." upload is complete<br>";
}
else
{
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
?>

dapat gagana nayan, hehe ayusin mo nalang.
 
Last edited:
Back
Top Bottom