Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Walidacja zdjęć + formularz
#1
Cześć wszystkim

Bardzo proszę was o pomoc bo ogarnęło mnie totalne zaciemnienie.
Mój problem polega na tym że pobieram z formularza a dokładniej z jego czterech pól cztery zdjęcia i zapisuję je do katalogu, ale totalnie nie mam pojęcia jak weryfikować czy zdjęcia są zdjęciami (jpg, jpeg, png, bmp) i jak wprowadzić ograniczenie dla wielkości np 1mb. Dla jednego pliku wiem jak to zrobić ale z czterema... bam pusto w głowie.

Bardzo proszę o waszą pomoc.

Kod PHP:
if (isset($_POST['submit'])){
            
//łaczy sie do bazy i wgrywa zdjecia i zapisuje je w folderze

                
$galeria_zdjecie1 $_FILES['file1']['name'];
                
$galeria_zdjecie2 $_FILES['file2']['name'];
                
$galeria_zdjecie3 $_FILES['file3']['name'];
                
$galeria_zdjecie4 $_FILES['file4']['name'];

                
// zapis do bazy danych
                
(...)          
                
// jesli uda sie zapisac teksty do bazy to zapisze zdjecie do folderu
                
if(($rowNo $db->exec($sql)) == FALSE){
                    
move_uploaded_file($_FILES['file1']['file2']['file3']['file4']['tmp_name'],
                    
$_SERVER['DOCUMENT_ROOT'].'/images/'.$_FILES['file1']['file2']['file3']['file4']['name']);

                    
header("location:../index.php");
                }else{
                    
// jesli nie to wyswietli blad
                    
$error "nie udało się zapisać zdjęcia";
                }

       
//jesli nie to pokaz blad     
}else{
    
$error "problem z wgraniem zdjęcia";

Odpowiedz
#2
Kod PHP:
$_FILES["file"]["type"]
$_FILES["file"]["size"
Odpowiedz
#3
Odpowiedz
#4
a pokaż cały kod (formularz i php)
Odpowiedz
#5
Kod PHP:
<?php
session_start
();
require(
"config.php");

/*
     *
     * @ Multiple File upload script.
     *
     * @ Can do any number of file uploads
     * @ Just set the variables below and away you go
     *
     * @ Author: Kevin Waterson
     *
     * @copywrite 2008 PHPRO.ORG
     *
     */

    
error_reporting(E_ALL);
 
    
/*** the upload directory ***/
    
$upload_dir'/uploads/';

    
/*** numver of files to upload ***/
    
$num_uploads 2;

    
/*** maximum filesize allowed in bytes ***/
    
$max_file_size  1048576;

    
/*** a message for users ***/
    
$msg 'Please select files for uploading';

    
/*** an array to hold messages ***/
    
$messages = array();

    
/*** check if a file has been submitted ***/
    
if(isset($_FILES['userfile']['tmp_name']))
    {
        
/** loop through the array of files ***/
        
for($i=0$i count($_FILES['userfile']['tmp_name']);$i++)
        {

            
// check if there is a file in the array
            
if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
            {
                
$messages[] = 'No file uploaded @';
            }
            
// check the file is less than the maximum file size
            
elseif($_FILES['userfile']['size'][$i] > $max_file_size)
            {
                
$messages[] = "File size exceeds $max_file_size limit";
            }
            else
            {   
                
                
// copy the file to the specified dir 
                
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$_SERVER['DOCUMENT_ROOT'].$upload_dir.$_FILES['userfile']['name'][$i]))
                {
                    
/*** give praise and thanks to the php gods ***/
                    
$messages[] = $_FILES['userfile']['name'][$i].' uploaded';
                }
                else
                {
                    
/*** an error message ***/
                    
$messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
                }
            }
        }
    }

?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
  


<div class="message">
     <h3><?php echo $msg?></h3>
     <p>
     <?php
        
if(sizeof($messages) != 0)
        {
            foreach(
$messages as $err)
            {
                echo 
$err.'<br />';
            }
        }
     
?>
     </p>
</div>

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
    <p>News Title:</p>
    <input type="text" name="article_title"/><br />

    <p>Wybierz zdjęcia:</p>
     <?php
        $num 
0;
        while(
$num $num_uploads)
        {
            echo 
'<input type="file" name="userfile[]"><br />';
            
$num++;
        }
     
?>
    <br />
    <input type="submit" name="submit">
    <input type="reset" value="Reset">
</form>

          
</body>
</html> 
Odpowiedz
#6
to pomoże
Kod PHP:
/** loop through the array of files ***/
        
for($i=0$i count($_FILES['userfile']['tmp_name']);$i++)
        {
if(!empty(
$_FILES['userfile']['name'][$i])) {        
if((
$_FILES['userfile']['type'][$i] == "image/jpg") || ($_FILES['userfile']['type'][$i] == "image/jpeg") || ($_FILES['userfile']['type'][$i] == "image/png") || ($_FILES['userfile']['type'][$i] == "image/gif") || ($_FILES['userfile']['type'][$i] == "image/pjpeg")) {
            
            
// check if there is a file in the array
            
if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
            {
              
$messages[] = 'No file uploaded @';
            }
            
// check the file is less than the maximum file size
            
elseif($_FILES['userfile']['size'][$i] > $max_file_size)
            {
              
$messages[] = "File size exceeds $max_file_size limit";
            }
            else
            {   
              
              
// copy the file to the specified dir 
              
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$_SERVER['DOCUMENT_ROOT'].$upload_dir.$_FILES['userfile']['name'][$i]))
              {
                
/*** give praise and thanks to the php gods ***/
                
$messages[] = $_FILES['userfile']['name'][$i].' uploaded';
              }
              else
              {
                
/*** an error message ***/
                
$messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
              }
            }
        }
}
        } 
Odpowiedz
#7
Dziękuję za pomoc.
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  Jak zrobic Formularz Kontaktowy na stronie ? Kodarth 191 255,832 06-04-2021, 16:25
Ostatni post: zerin
  Weryfikacja zdjęć na stronie widzaceoko 0 1,703 19-02-2018, 02:26
Ostatni post: widzaceoko
  Jak zrobić prostą przeglądarkę zdjęć? messi19125 3 4,293 21-07-2014, 02:41
Ostatni post: Kartofelek
  php formularz - wszystkie pola muszą być wypełnione d3d3d3 2 3,634 01-10-2013, 14:16
Ostatni post: Kartofelek
  [PHP] Formularz mailowy - wysyłanie wiadomości spolprog 7 6,486 05-09-2013, 17:13
Ostatni post: Engine

Skocz do:


Użytkownicy przeglądający ten wątek: 1 gości
Sponsorzy i przyjaciele
SeoHost.pl