logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 01-20-2015, 11:57 AM   #1
neodev
 
Join Date: Jan 2015
Posts: 25
Default Dreamweaver form upload with text

Hi there,
I build a form with 2 text field and one upload field (image). It send data into a mysql db called "images" with a table called "pictures" with 4 fields:"id", "name", "surname" and "immagine". The script resize the image on upload into 3 different format (600px,200px,100px) and upload them into 3 folder:large, small and micro.It work in a excellent way (for the resize goal) but I'm unable to store the "name and surname" field and the "path/image name"into the mysql db.Also, I'm unable to upload the next image. Please, could you help me? Thanks.


PHP Code:


<?php include('connect.php');?>


<?php  

// function to get file extension 
function getExtension($str) { 

         
$i strrpos($str,"."); 
         if (!
$i) { return ""; }  
         
$l strlen($str) - $i
         
$ext substr($str,$i+1,$l); 
         return 
$ext

// define max file size, change to suit your own maximum file size 
define ("MAX_SIZE","10000"); 
$errors=0


//get the original file from form 
if($_SERVER["REQUEST_METHOD"] == "POST"



// from your upload form 

$image =  $_FILES["file"]["name"];// from your upload form 
$uploadedfile $_FILES['file']['tmp_name'];// from your upload form 
// check for correct file extension 
if ($image)  

$filename  stripslashes($_FILES['file']['name']); 
$extension getExtension($filename); 
$extension strtolower($extension); 
if ((
$extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))  

echo 
' Unknown Image extension! ';// build your own error handling here 
$errors 1

// check for correct file size 
else 

$size=filesize($_FILES['file']['tmp_name']); 
  
if (
$size MAX_SIZE*1024

 echo 
"You have exceeded the size limit";// build your own error handling here 
 
$errors 1

// check which extension we have and create file 
if($extension=="jpg" || $extension=="jpeg" 

$uploadedfile $_FILES['file']['tmp_name']; 
$src imagecreatefromjpeg($uploadedfile); 

else if(
$extension=="png"

$uploadedfile $_FILES['file']['tmp_name']; 
$src imagecreatefrompng($uploadedfile); 

else  

$src imagecreatefromgif($uploadedfile); 

//resize image and create thumbs and small thumbs  
list($width,$height)=getimagesize($uploadedfile); 
//for main images, this keeps the width at 300px regardless of portrait or landscape 
$newwidth=600
$newheight=($height/$width)*$newwidth
$tmp=imagecreatetruecolor($newwidth,$newheight); 
//for thumbnails, change the $newwidth variable to what ever size you need 
if($width $height){//landscape 
$newwidth1=200
$newheight1=($height/$width)*$newwidth1
$tmp1=imagecreatetruecolor($newwidth1,$newheight1); 
}elseif(
$width $height){//portrait 
$newheight1=100
$newwidth1=($width/$height)*$newheight1
$tmp1=imagecreatetruecolor($newwidth1,$newheight1); 

//for smaller thumbnails, my site required this smaller thumb. Delete this section if you dont need it 
if($width $height){//landscape 
$newwidth2=50
$newheight2=($height/$width)*$newwidth2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2); 
}elseif(
$width $height){//portrait 
$newheight2=50
$newwidth2=($width/$height)*$newheight2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2); 

// create new resized files 
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); 

imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height); 
//declare destinations 
$filename  "large/"$_FILES['file']['name'];                   
$filename1 "small/"$_FILES['file']['name'];        // your destination paths here 
$filename2 "micro/"$_FILES['file']['name'];    
//write files to folders 
imagejpeg($tmp,$filename,100); 
imagejpeg($tmp1,$filename1,100); 
imagejpeg($tmp2,$filename2,100);
//destroy temps to save memory 
imagedestroy($src); 
imagedestroy($tmp); 
imagedestroy($tmp1); 
imagedestroy($tmp2); 




//If no errors registered, insert file name into database and print the success message 
if(isset($_POST['invia']) && !$errors)  

mysql_query("INSERT INTO pictures VALUES ('$_POST[nome],'$src)");  

$success    =    "Image Uploaded Successfully!"

//////////////////////////////////////////////////select file names from database 
 
?> 

 






  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Upload Image Files and Resize</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 
<form name="upload" enctype="multipart/form-data" method="post" action="upload.php"> 
  <p>Name :</p>
  <p>
    <label for="name"></label>
    <input type="text" name="name" id="name">
  </p>
  <p>Surname:</p>
  <p>
    <label for="surname"></label>
    <input type="text" name="surname" id="surname">
  </p>
  <p>Image:</p>
  <p>
    <input type="file" name="file" id="file" >
  </p>
  <p>
    <input type="submit" name="invia" id="invia" value="Submit">
  </p> 
</form> 
</body> 
</html>
neodev is offline   Reply With Quote
Old 01-21-2015, 02:04 AM   #2
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

quick look only but comment reads
// create new resized files

//declare destinations
$filename = "large/". $_FILES['file']['name'];
$filename1 = "small/". $_FILES['file']['name']; // your destination paths here
$filename2 = "micro/". $_FILES['file']['name'];



so they are the paths that you want inserting into your database
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 01-21-2015, 08:53 AM   #3
neodev
 
Join Date: Jan 2015
Posts: 25
Default

Hi edbr,
thanks for your reply.
Yes, I need to put the three images path into database , but the question is that I can't insert the other 2 field into the database (name e surname).
The easyphp server (where I test this script), give me back always the same error in the mysql_query:
mysql_query("INSERT INTO pictures VALUES ('$_POST[nome],'$src)");

Thanks for your help.
neodev is offline   Reply With Quote
Old 01-21-2015, 09:02 AM   #4
neodev
 
Join Date: Jan 2015
Posts: 25
Default

Just change the "mysql_query" in this way:

mysql_query("INSERT INTO pictures VALUES ('name','surname','src)");

No more error is displayed on the page preview, but still not insert nothing into the db
neodev is offline   Reply With Quote
Old 01-22-2015, 01:03 AM   #5
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

mysql_query("INSERT INTO pictures (name.surname,image) VALUES ('name','surname','src)");
try that, edit to your datanase firld names
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 01-22-2015, 09:36 AM   #6
neodev
 
Join Date: Jan 2015
Posts: 25
Default

I really thank you for support. I have insert your code: no more error when I test the page. The pictures are upload perfectly in the three folder with resized formats. But the data insert into "name and surname" field are not into the database. A this point, I start thinking about a "phpMyadmin" problem. Because when I tested other form, sometimes the data appear into phpMyadmin, others no. I use "EasyPHP-DevServer-14.1VC11". Please, have you some advice for me?
Thanks again, I'll try totest again
neodev is offline   Reply With Quote
Old 01-22-2015, 05:57 PM   #7
neodev
 
Join Date: Jan 2015
Posts: 25
Default

Hi edbr,
First of all, I unistall easyphp and install wamp server. I make several test on simple database connection and everything is fine.
I tryed several times to edit the code as you told me, but nothing. As I said, the resize script work great, but I can't send the data into the db.
Hope you can help me. Thanks.
neodev is offline   Reply With Quote
Old 01-22-2015, 07:08 PM   #8
neodev
 
Join Date: Jan 2015
Posts: 25
Default

I found a tutorial on the net to build what I need. Now I need to include only the resize script into the "cone_noti.php" file. Hope you can help me.

The form ("insertanoti.php"):
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insertar</title>
</head>

<body>
<h1>Insertar noticia</h1>
<form action="cone_noti.php" method=post enctype="multipart/form-data">


<label>
  TITULO:
  <input name="titulo" type="text" id="titulo">
  </label>
  <br>

<label>
  FECHA:   
  <input name="fecha" type="text" id="fecha">
  </label>
  <br>

<label>
  DESCRIPCION:
    <input name="descripcion" type="text" id="descripcion" value="" />
</label>
  <br>
  <label>
  NOTICIA:
    <input name="noticia" type="text" id="noticia" value="" />
</label>
  <br>


Elija Foto de Perfil<br>

 
<input type="file" name="imagen">
<input type="submit" value="Guardar">
</form>
<p>Nota: El formato de la fecha (aņo/mes/dia)</p>
<br />
<a href="index.php">Atras</a>
<body>
</body>
</html>
=================================

"cone_noti.php"

PHP Code:

<?php
session_start
();
$titulo=$_POST['titulo'];
$fecha=$_POST['fecha'];
$descripcion=$_POST['descripcion'];
$noticia=$_POST['noticia'];
$nameimagen $_FILES['imagen']['name'];
$tmpimagen $_FILES['imagen']['tmp_name'];
$extimagen pathinfo($nameimagen);
$ext = array("png","gif","jpg");
$urlnueva "imagenes/".$nameimagen;



if(
is_uploaded_file($tmpimagen)) {
        if(
array_search($extimagen['extension'],$ext)){
            
copy($tmpimagen,$urlnueva);
            





mysql_connect("localhost","root");
 
mysql_select_db("prueba1");  
 
mysql_query("INSERT INTO noticias(titulo,fecha,descripcion,noticia,imagen) values('$titulo','$fecha','$descripcion','$noticia','$urlnueva')");
 
header("Location:insertarnoti.php");

            echo 
"Se ha guardado correctamente";


        } else {
            echo 
"Error: Solo imagenes con formato (jpg, png o gif)";
        }
} else {
    echo 
"Elija una imagen";
}

?>
======================================

the resize script to include:

PHP Code:


// function to get file extension 
function getExtension($str) { 

         
$i strrpos($str,"."); 
         if (!
$i) { return ""; }  
         
$l strlen($str) - $i
         
$ext substr($str,$i+1,$l); 
         return 
$ext

// define max file size, change to suit your own maximum file size 
define ("MAX_SIZE","10000"); 
$errors=0


//get the original file from form 
if($_SERVER["REQUEST_METHOD"] == "POST"



// from your upload form 

$image =  $_FILES["file"]["name"];// from your upload form 
$uploadedfile $_FILES['file']['tmp_name'];// from your upload form 
// check for correct file extension 
if ($image)  

$filename  stripslashes($_FILES['file']['name']); 
$extension getExtension($filename); 
$extension strtolower($extension); 
if ((
$extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))  

echo 
' Unknown Image extension! ';// build your own error handling here 
$errors 1

// check for correct file size 
else 

$size=filesize($_FILES['file']['tmp_name']); 
  
if (
$size MAX_SIZE*1024

 echo 
"You have exceeded the size limit";// build your own error handling here 
 
$errors 1

// check which extension we have and create file 
if($extension=="jpg" || $extension=="jpeg" 

$uploadedfile $_FILES['file']['tmp_name']; 
$src imagecreatefromjpeg($uploadedfile); 

else if(
$extension=="png"

$uploadedfile $_FILES['file']['tmp_name']; 
$src imagecreatefrompng($uploadedfile); 

else  

$src imagecreatefromgif($uploadedfile); 

//resize image and create thumbs and small thumbs  
list($width,$height)=getimagesize($uploadedfile); 
//for main images, this keeps the width at 300px regardless of portrait or landscape 
$newwidth=600
$newheight=($height/$width)*$newwidth
$tmp=imagecreatetruecolor($newwidth,$newheight); 
//for thumbnails, change the $newwidth variable to what ever size you need 
if($width $height){//landscape 
$newwidth1=200
$newheight1=($height/$width)*$newwidth1
$tmp1=imagecreatetruecolor($newwidth1,$newheight1); 
}elseif(
$width $height){//portrait 
$newheight1=100
$newwidth1=($width/$height)*$newheight1
$tmp1=imagecreatetruecolor($newwidth1,$newheight1); 

//for smaller thumbnails, my site required this smaller thumb. Delete this section if you dont need it 
if($width $height){//landscape 
$newwidth2=50
$newheight2=($height/$width)*$newwidth2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2); 
}elseif(
$width $height){//portrait 
$newheight2=50
$newwidth2=($width/$height)*$newheight2
$tmp2=imagecreatetruecolor($newwidth2,$newheight2); 

// create new resized files 
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); 

imagecopyresampled($tmp2,$src,0,0,0,0,$newwidth2,$newheight2,$width,$height); 
//declare destinations 
$filename  "large/"$_FILES['file']['name'];                   
$filename1 "small/"$_FILES['file']['name'];        // your destination paths here 
$filename2 "micro/"$_FILES['file']['name'];    
//write files to folders 
imagejpeg($tmp,$filename,100); 
imagejpeg($tmp1,$filename1,100); 
imagejpeg($tmp2,$filename2,100);
//destroy temps to save memory 
imagedestroy($src); 
imagedestroy($tmp); 
imagedestroy($tmp1); 
imagedestroy($tmp2); 




//If no errors registered, insert file name into database and print the success message 
if(isset($_POST['invia']) && !$errors)  

mysql_query("INSERT INTO pictures(name,surname,images) VALUES ('$_POST[name]','$_POST[surname]','src)"); 



 
$success    =    "Image Uploaded Successfully!"

//////////////////////// 
neodev is offline   Reply With Quote
Old 01-23-2015, 01:55 AM   #9
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

first check your connection, i see you dont have a password which might throw it out. make a file 1.e conn.php save in your site folder.
add
Code:
<?php

mysql_connect("localhost", "root" , " ") or die(mysql_error());

?>
then call it in your browser and see if error reported.
it is a better practice to add a user and password to your database anyway as it will be so in a live situation
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 01-23-2015, 04:26 AM   #10
neodev
 
Join Date: Jan 2015
Posts: 25
Default

Hi edbr,
yes, I have create a "Connection/noticias.php" folder in dreamweaver and I have included it on the page where I show the "insert result, as follow:


(this is the connection script created by dreamweaver:

PHP Code:

<?php

error_reporting
(E_ALL E_DEPRECATED);

# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_noticias "localhost";
$database_noticias "prueba1";
$username_noticias "root";
$password_noticias "";
$noticias mysql_pconnect($hostname_noticias$username_noticias$password_noticias) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
and this is the "show records page":

PHP Code:

<?php require_once('Connections/noticias.php'); ?>

<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$maxRows_rs_noticias 10;
$pageNum_rs_noticias 0;
if (isset(
$_GET['pageNum_rs_noticias'])) {
  
$pageNum_rs_noticias $_GET['pageNum_rs_noticias'];
}
$startRow_rs_noticias $pageNum_rs_noticias $maxRows_rs_noticias;

mysql_select_db($database_noticias$noticias);
$query_rs_noticias "SELECT * FROM noticias";
$query_limit_rs_noticias sprintf("%s LIMIT %d, %d"$query_rs_noticias$startRow_rs_noticias$maxRows_rs_noticias);
$rs_noticias mysql_query($query_limit_rs_noticias$noticias) or die(mysql_error());
$row_rs_noticias mysql_fetch_assoc($rs_noticias);

if (isset(
$_GET['totalRows_rs_noticias'])) {
  
$totalRows_rs_noticias $_GET['totalRows_rs_noticias'];
} else {
  
$all_rs_noticias mysql_query($query_rs_noticias);
  
$totalRows_rs_noticias mysql_num_rows($all_rs_noticias);
}
$totalPages_rs_noticias ceil($totalRows_rs_noticias/$maxRows_rs_noticias)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>index</title>
</head>
<a href="insertarnoti.php">Insertar noticia</a>
<br />



<body>
<table border="1">
  <tr>
    <td>titulo</td>
    <td>fecha</td>
    <td>descripcion</td>
    <td>noticia</td>
    <td>imagen</td>
  </tr>
 
    <?php do { ?>
      <tr>
        <td><?php echo $row_rs_noticias['titulo']; ?></td>
        <td><?php echo $row_rs_noticias['fecha']; ?></td>
        <td><?php echo $row_rs_noticias['descripcion']; ?></td>
        <td><?php echo $row_rs_noticias['noticia']; ?></td>
        <td><img src="<?php echo $row_rs_noticias['imagen']; ?>" width="120px" height="120px" /></td>
      </tr>
      <?php } while ($row_rs_noticias mysql_fetch_assoc($rs_noticias)); ?>
   
</table>
</body>
</html>
<?php
mysql_free_result
($rs_noticias);

?>

The connection file is included only in the "show record" page.


As I wrote, all work fine, text field in form pass all the data into database. I miss to insert the script that resize the images into the three folder.

Thanks for support, I hope to see your reply soon.
neodev is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:31 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com