Jump to content

upload images with php


luismc2007

Recommended Posts

Hi: I´m trying to make my own code for upload images using php to my server for my webpage, but I don´t know why the code doesn´t work at 100%.

I know that its works until a point but not more away...

Can you help me??

 

I have two files: an html and php called "form.html" and "sube.php" that I post here...

And now explain what I´m try to do...

 

[ File: form.html ]

 

<form action="sube.php" method="post" enctype="multipart/form-data">
Sube un archivo:<br />
<input type="file" name="archivo" id="archivo" />
<br  />
<input type="submit" name="boton" value="Subir" />
</form>

 

[ File: sube.php ]

 

<?php
echo "Name of image: ".$_FILES["archivo"]["name"];
echo "<br />";

$tamano = $_FILES["archivo"]["size"];
echo "Size of image: ".$_FILES["archivo"]["size"]; // here always an error
echo "<br>";

$t_max="50000000000";
echo "Maximum size:";
echo "$t_max <br>";

if( $tamano < $t_max)
{
$destino = "fotogal/grandes";
$sep=explode('image/',$_FILES["archivo"]["type"]);

if($_FILES["archivo"]["type"] == "gif" || $_FILES["archivo"]["type"] == "jpg" )
 move_uploaded_file ( $_FILES["archivo"]["tmp_name"], $destino . '/' .$_FILES["archivo"]["name"]);
 }
else echo "No way..."; // OUT
}
else
 {
 echo "too much weight of image.";
 }
 

?>

 

The case of this is that always i reach the same point and don´t know how to go further...

The point is that always I receive:

Name.."ok"...

Size: This item is completely different of the weight of the image

And...

No way.

 

This is always my result.

 

Can you help me with this code????? I would like to upload my own images, but I´m not able to do it... and don´t know why.

 

Thanks a lot.

regards

 

 

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/278205-upload-images-with-php/
Share on other sites

There are erors in your sube.php file:

 

Try this:

 

sube.php

<?php

echo "Name of image: " . $_FILES["archivo"]["name"];
echo "<br />";

$tamano = $_FILES["archivo"]["size"];
echo "Size of image: " . $_FILES["archivo"]["size"]; // here always an error
echo "<br>";

$t_max = "50000000000";
echo "Maximum size:";
echo "$t_max <br>";

echo "File type:";
echo  $_FILES["archivo"]["type"].'<br />';

if ($_FILES['archivo']['error'] === 0) {
    if ($tamano < $t_max) {
        
        $destino = "fotogal/grandes";
        
        //get a file extension
        $file_ext = basename($_FILES["archivo"]['name']);
        
        // check the file extension in php
        $pattern = '/^.*\.(jpg|jpeg|gif)$/i';
        
        if (!preg_match($pattern, $file_ext)) {
                echo "ERROR: You must select a valid image file!"; exit;
            }

        if(move_uploaded_file($_FILES["archivo"]["tmp_name"], $destino . '/' . $_FILES["archivo"]["name"])){
            echo "Upload success";
         } else {
             echo 'Upload failed.'; 
         } 
    } else {

        echo "too much weight of image.";
    }
} else {
    echo "ERROR: Some error(s) was finded in that file";     
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.