Question upload

Signaler
Messages postés
2
Date d'inscription
jeudi 2 juin 2005
Statut
Membre
Dernière intervention
23 août 2006
-
Messages postés
2
Date d'inscription
jeudi 2 juin 2005
Statut
Membre
Dernière intervention
23 août 2006
-
Bonjour à tous,

Débutant en PHP ,j'ai un scxcript upload pour mon forum mais le soucis j'arrive pas a avoir le lien de téléchargement apres voici le script en question :
<?php

/* loading language file */
function get_translations($lngfile) {

  if (file_exists($lngfile)) {
    $lines = file($lngfile);
    while (list(,$line) = each($lines)) {
      list($key,$val)= explode("=",$line);
          $phrases[$key] = $val;
    }
    return $phrases;
  } else {
    return false;
  }
}
/* translate a string */
function translate($str,$vals="") {

  global $phrases;

  if ($phrases) {
    $p_str = $phrases[$str];
        if ($p_str == "") {
          $p_str = $str;
        }
  } else {
    $p_str = $str;
  }

  if (gettype($vals)=="array") {
    $p_str = sprintf($p_str,$vals[0],$vals[1],$vals[2],$vals[3],$vals[4],$vals[5],$vals[6]);
  }

  return (trim($p_str));
}

$my_max_file_size         = "102400"; # in bytes
$image_max_width        = "800";
$image_max_height        = "800";
$the_path                        = "image/";

$registered_types = array(
                                        "application/x-gzip-compressed"         => ".tar.gz, .tgz",
                                        "application/x-zip-compressed"          => ".zip",
                                        "application/x-tar"                     => ".tar",
                                        "text/plain"                            => ".html, .php, .txt, .inc (etc)",
                                        "image/bmp"                             => ".bmp, .ico",
                                        "image/gif"                             => ".gif",
                                        "image/pjpeg"                           => ".jpg, .jpeg",
                                        "image/jpeg"                            => ".jpg, .jpeg",
                                        "application/x-shockwave-flash"         => ".swf",
                                        "application/msword"                    => ".doc",
                                        "application/vnd.ms-excel"              => ".xls",
                                        "application/octet-stream"              => ".exe, .fla (etc)"
                                        );

$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg");

# --

function form($error=false) {

global $PHP_SELF,$my_max_file_size,$lng;

        if ($error) print $error . "

";

        print "\n<form ENCTYPE="multipart/form-data"  action="upload.php3" method="post">";
        print "\n";
        print "\n";
        print "\n".translate("Upload images")."";
        print "\n
NOTE: ".translate("Max file size is")." " . ($my_max_file_size / 1024) . "KB";
         print "\n

";
        print "\n";
        print "\n</form>";

} # END form

# --

if (!ereg("^4",phpversion())) {
        function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
                for ($i=0; $i < count($haystack); $i++) {
                        if ($haystack[$i] == $needle) {
                                return true;
                        }
                }
        }
}

# --

function validate_upload($the_file) {

global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;

        $start_error = "\n Error: \n";

        if ($the_file == "none") { # do we even have a file?

                $error .= "\n<li>You did not upload anything!</li>";

        } else { # check if we are allowed to upload this file_type

                if (!in_array($the_file_type,$allowed_types)) {
                        $error .= "\n<li>The file that you uploaded was of a type that is not allowed, you are only
                                                allowed to upload files of the type:\n";
                        while ($type = current($allowed_types)) {
                                $error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
                                next($allowed_types);
                        }
                        $error .= "\n";
                }

                if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {

                        $size = GetImageSize($the_file);
                        list($foo,$width,$bar,$height) = explode(""",$size[3]);

                        if ($width > $image_max_width) {
                                $error .= "\n<li>Your image should be no wider than " . $image_max_width . " Pixels</li>";
                        }

                        if ($height > $image_max_height) {
                                $error .= "\n<li>Your image should be no higher than " . $image_max_height . " Pixels</li>";
                        }

                }

                if ($error) {
                        $error = $start_error . $error . "\n";
                        return $error;
                } else {
                        return false;
                }
        }
} # END validate_upload

# --

function list_files() {

global $the_path;

        $handle = dir($the_path);
        print "\n Uploaded files:
";
        while ($file = $handle->read()) {
                if (($file != ".") && ($file != "..")) {
                        print "\n" . $file . "
";
           }
        }
        print "<hr>";
}

# --

function upload($the_file) {

global $the_path,$the_file_name;

        $error = validate_upload($the_file);
        if ($error) {
                form($error);
        } else { # cool, we can continue
                if (!@copy($the_file, $the_path . "/" . $the_file_name)) {
                        form("\n Something barfed, check the path to and the permissions for the upload directory ");
                } else {
                        list_files();
                        form();
                }
        }
} # END upload

# --

############ Start page

print "<html>\n<head>\n<title>Upload example</title>\n</head>\n";
echo("  ----

    ");
switch($task) {
        case 'upload':
                upload($the_file);
        break;
        default:
                form();
}
echo(",
 
");
print "\n\n</html>";

?>

je dérerai implanter la fonction suivante pour recuperer le lien :
{
if(move_uploaded_file($_FILES['fichier']['tmp_name'],$directory.$nom_du_fichier.$ext)) {
$filename = $nom_du_fichier.$ext;
exit("Envoi du fichier effectué avec succès.
Adresse du fichier: ");
}
Mais je sais pas ou l inclure exactement.

Merci de votre aide
romu58

2 réponses

Messages postés
10839
Date d'inscription
lundi 24 février 2003
Statut
Modérateur
Dernière intervention
2 mars 2010
26
Hello,

burk, trop long à lire.
Voici un exemple tiré d'un petit script à moi (j'ai enlevé la partie traitement de fichiers):

<?php
$bDone = false;
if (isset ($_POST['valid']) && $_POST['valid'] === 'Soumettre') {
 $fromDir = 'from';
 $toDir = 'to';
 if (!file_exists ($fromDir)) {
  mkdir ($fromDir, 0755);
 }
 if (!file_exists ($toDir)) {
  mkdir ($toDir, 0755);
 }
 if (isset ($_FILES['fichier']) && empty ($_FILES['fichier']['error'])) {
  @move_uploaded_file ($_FILES['fichier']['tmp_name'], $fromDir.'/'.basename ($_FILES['fichier']['name']));
  // traitement sur le fichier ici
  // bla bla, puis écriture du fichier modifié dans un autre répertoire :
  $fp = fopen ($toDir.'/'.basename ($_FILES['fichier']['name']), 'w+');
  fwrite ($fp, $sString);
  fclose ($fp);
  $bDone = true;
 }
}
?>
<?xml version="1.0" encoding="ISO 8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
 <head>
 <link rel="stylesheet" type="text/css" href="css/main.css" />
 <meta http-equiv="Content-Type" content="text/html; charset=ISO 8859-1" />
 </head>
 
  <form method="post" enctype="multipart/form-data" action="">
  <fieldset>
   <legend>Parser SMS CELLCAST</legend>
   
    ^
   
   
    <label for="idFichier">Fichier &agrave; modifier</label>
    
    
   

  </fieldset>
  </form>
  
Fichier modifié :

  <?php if (true === $bDone) : ?>
   [<?php echo $toDir.'/'.basename ($_FILES['fichier']['name']); ?> <?php echo basename ($_FILES['fichier']['name']); ?>]
  <?php endif; ?>
  

 
</html>
Messages postés
2
Date d'inscription
jeudi 2 juin 2005
Statut
Membre
Dernière intervention
23 août 2006

merci

Je test et vous je vous dit mais merci encore pour votre aide

romu58