php中的文件上传脚本无法正常工作

时间:2017-07-04 05:50:43

标签: php

我的文件上传的PHP代码不起作用。我以各种方式尝试过它。我的代码如下:
HTML代码:

<form action="assets/php/regc.php" method="POST" name="rform" enctype="multipart/form-data">
<input name="photo" type="file" placeholder="Attach Photo" required="required"/><br>
<input type="submit" value="upload" name="" />

</form>

PHP Code:
<?php
$pic=$_FILES["photo"]["name"];

$location="assets/";
$myfile= $location.basename($_FILES["photo"]["name"]);

if(isset($_FILES["photo"]["name"]))
{
    $ok=move_uploaded_file($_FILES["photo"]["tmp_name"], $myfile);
    if($ok)
    {
    echo "success"; 
    }

    else
    {
    echo "Failed";  
    }
}
?>

3 个答案:

答案 0 :(得分:0)

public class MyService extends Service {

    private Timer timer;
    private TimerTask timerTask;

    @Override
    public void onCreate() {
        super.onCreate();
        startTimer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
//        startTimer();
//        sendNotification();
        return START_STICKY;
    }


    public void startTimer() {
        //set a new Timer
        timer = new Timer();

        //initialize the TimerTask's job
        initializeTimerTask();

        //schedule the timer, to wake up every 10 second
        timer.schedule(timerTask, 5000, 5000); //
    }

    /**
     * it sets the timer to print the counter every x seconds
     */
    public void initializeTimerTask() {
        timerTask = new TimerTask() {
            public void run() {
//                Log.e("in timer", "in timer ++++  " + (counter++));

                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                ""+ (counter++),
                                Toast.LENGTH_SHORT).show();
                    }
                });
//                Toast.makeText(SensorService.this, ""+ (counter++), Toast.LENGTH_SHORT).show();
            }
        };
    }

    /**
     * not needed
     */
    public void stoptimertask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}
输入标记中的

也会传递id

答案 1 :(得分:0)

尝试这种方式:

    <?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

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

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if($file_size > 2097152) {
         $errors[]='File size must be excately 2 MB';
      }

      if(empty($errors)==true) {
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>

      <form action = "" method = "POST" enctype = "multipart/form-data">
         <input type = "file" name = "image" />
         <input type = "submit"/>

         <ul>
            <li>Sent file: <?php echo $_FILES['image']['name'];  ?>
            <li>File size: <?php echo $_FILES['image']['size'];  ?>
            <li>File type: <?php echo $_FILES['image']['type'] ?>
         </ul>

      </form>

   </body>
</html>

答案 2 :(得分:0)

你可以尝试一下。它看起来很相似 https://www.w3schools.com/php/php_file_upload.asp